Use killable vma write locking in most places
Subsystems (12)
Suren Baghdasaryan
reviewerv5 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.
Changes since v4 [1]:
- added Reviewed-by, per Barry Song and Lorenzo Stoakes (wherever the code stayed the same)
- split patch 2 into 3 parts, per Lorenzo Stoakes
- converted vma_start_write() in mseal_apply(), per Sashiko
- changed vma_start_write_killable() error handling in set_mempolicy_home_node(), per Lorenzo Stoakes
- added comment why mm->locked_vm is fine even when we exit early, per Sashiko
- moved vma locking before vrm_calc_charge() in move_vma(), per Sashiko and Lorenzo Stoakes
- set give_up_on_oom on error in vma_merge_existing_range() to propagate the error, per Lorenzo Stoakes
- moved validate_mm() out of the error path in expand_upwards(), per Lorenzo Stoakes
- dropped the patch changing S390 error handling, per Sashiko and Lorenzo Stoakes
- reworked error handling in clear_refs_write(), per Lorenzo Stoakes
- uninlined process_vma_walk_lock() while changing its return type, per Lorenzo Stoakes
[1] https://lore.kernel.org/all/[email protected]/
Suren Baghdasaryan (6):
mm/vma: cleanup error handling path in vma_expand()
mm: use vma_start_write_killable() in mm syscalls mm/khugepaged: use vma_start_write_killable() in collapse_huge_page() mm/vma: use vma_start_write_killable() in vma operations
mm: use vma_start_write_killable() in process_vma_walk_lock()
KVM: PPC: use vma_start_write_killable() in
kvmppc_memslot_page_merge()
arch/powerpc/kvm/book3s_hv_uvmem.c | 5 +- fs/proc/task_mmu.c | 12 +-- 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 | 8 +- mm/mseal.c | 5 +- mm/pagewalk.c | 22 +++-- mm/vma.c | 146 +++++++++++++++++++++-------- mm/vma_exec.c | 6 +- 13 files changed, 190 insertions(+), 70 deletions(-)
base-commit: e53c9040ab1b738dd2c83b57558f141902caaf4f
Signature · 1 line
2.53.0.1018.g2bb0e51243-goog
Suren Baghdasaryan
reviewerPATCH v5 1/6 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
@@ -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
Suren Baghdasaryan
reviewerPATCH v5 2/6 mm: use vma_start_write_killable() in mm syscalls
In reply to Suren BaghdasaryanReplace vma_start_write() with vma_start_write_killable() in syscalls, improving reaction time to the kill signal.
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.
--- mm/madvise.c | 4 +++- mm/memory.c | 2 ++ mm/mempolicy.c | 11 +++++++++-- mm/mlock.c | 28 ++++++++++++++++++++++------ mm/mprotect.c | 5 ++++- mm/mremap.c | 8 +++++--- mm/mseal.c | 5 ++++- 7 files changed, 49 insertions(+), 14 deletions(-)
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 ; + vma->flags = new_vma_flags;if (set_new_anon_name)return replace_anon_vma_name (vma, anon_name);
mm/memory.c +2 -0
@@ -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 +9 -2
@@ -1784,7 +1784,8 @@ SYSCALL_DEFINE4(set_mempolicy_home_node, unsigned long, start, unsigned long, lereturn -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,19 @@ 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. +*/ + err =vma_start_write_killable (vma); +if (err) +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
@@ -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;/* mm->locked_vm is fine as nr_pages == 0 */ /* 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
@@ -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 +5 -3
@@ -1348,6 +1348,11 @@ static unsigned long move_vma(struct vma_remap_struct *vrm)if (err)return err; +/* We don't want racing faults. */ + err =vma_start_write_killable (vrm->vma); +if (err) +return err; +/* * If accounted, determine the number of bytes the operation will * charge.
@@ -1355,9 +1360,6 @@ static unsigned long move_vma(struct vma_remap_struct *vrm)if (!vrm_calc_charge (vrm))return -ENOMEM ; -/* We don't want racing faults. */ -vma_start_write (vrm->vma); -/* Perform copy step. */ err =copy_vma_and_data (vrm, &new_vma);/*
mm/mseal.c +4 -1
@@ -70,6 +70,7 @@ static int mseal_apply(struct mm_struct *mm,if (!vma_test (vma,VMA_SEALED_BIT )) {vma_flags_t vma_flags = vma->flags; +int err;vma_flags_set (&vma_flags,VMA_SEALED_BIT );
@@ -77,7 +78,9 @@ static int mseal_apply(struct mm_struct *mm, curr_end, &vma_flags);if (IS_ERR (vma))return PTR_ERR (vma); -vma_start_write (vma); + err =vma_start_write_killable (vma); +if (err) +return err;vma_set_flags (vma,VMA_SEALED_BIT ); }
Signature · 1 line
2.53.0.1018.g2bb0e51243-goog
Suren Baghdasaryan
reviewerPATCH v5 3/6 mm/khugepaged: use vma_start_write_killable() in collapse_huge_page()
In reply to Suren BaghdasaryanReplace vma_start_write() with vma_start_write_killable(), improving reaction time to the kill signal.
Replace vma_start_write() in collapse_huge_page().
--- mm/khugepaged.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
mm/khugepaged.c +4 -1
@@ -1166,7 +1166,10 @@ static enum scan_result collapse_huge_page(struct mm_struct *mm, unsigned long aif (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;
Signature · 1 line
2.53.0.1018.g2bb0e51243-goog
Suren Baghdasaryan
reviewerPATCH v5 4/6 mm/vma: use vma_start_write_killable() in vma operations
In reply to Suren BaghdasaryanReplace vma_start_write() with vma_start_write_killable(), improving reaction time to the kill signal.
Replace vma_start_write() calls when we operate on VMAs.
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.
--- mm/vma.c | 130 +++++++++++++++++++++++++++++++++++++------------- mm/vma_exec.c | 6 ++- 2 files changed, 101 insertions(+), 35 deletions(-)
mm/vma.c +97 -33
@@ -524,6 +524,17 @@ __split_vma(struct vma_iterator *vmi, struct vm_area_struct *vma, new->vm_pgoff += ((addr - vma->vm_start) >>PAGE_SHIFT ); } +/* +* 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; + 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); -init_vma_prep (&vp, vma); vp.insert = new;vma_prepare (&vp);
@@ -900,12 +908,22 @@ 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) { +/* Ensure error propagates. */ + vmg->give_up_on_oom = false; + goto abort; + }if (merge_right) {vma_flags_t next_sticky; -vma_start_write (next); + err =vma_start_write_killable (next); +if (err) { +/* Ensure error propagates. */ + vmg->give_up_on_oom = false; + goto abort; + } vmg->target = next; next_sticky =vma_flags_and_mask (&next->flags,VMA_STICKY_FLAGS );vma_flags_set_mask (&sticky_flags, next_sticky);
@@ -914,7 +932,12 @@ 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) { +/* Ensure error propagates. */ + vmg->give_up_on_oom = false; + goto abort; + } vmg->target = prev; prev_sticky =vma_flags_and_mask (&prev->flags,VMA_STICKY_FLAGS );
@@ -1170,10 +1193,12 @@ 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 err =0 ; +int err;mmap_assert_write_locked (vmg->mm); -vma_start_write (target); + err =vma_start_write_killable (target); +if (err) +return err; target_sticky =vma_flags_and_mask (&target->flags,VMA_STICKY_FLAGS );
@@ -1201,6 +1226,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; err =dup_anon_vma (target, next, &anon_dup);if (err)return err;
@@ -1214,7 +1246,6 @@ int vma_expand(struct vma_merge_struct *vmg)if (remove_next) {vma_flags_t next_sticky; -vma_start_write (next); vmg->__remove_next = true; next_sticky =vma_flags_and_mask (&next->flags,VMA_STICKY_FLAGS );
@@ -1252,9 +1283,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
@@ -1263,8 +1299,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); -init_vma_prep (&vp, vma);vma_prepare (&vp);vma_adjust_trans_huge (vma, start, end,NULL );
@@ -1453,7 +1487,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;mas_set (mas_detach, vms->vma_count++); error =mas_store_gfp (mas_detach, next,GFP_KERNEL );if (error)
@@ -1848,12 +1884,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;vma_iter_config (&vmi, vma->vm_start, vma->vm_end);if (vma_iter_prealloc (&vmi, vma))return -ENOMEM ; -vma_start_write (vma);vma_iter_store_new (&vmi, vma);vma_link_file (vma,/* hold_rmap_lock= */ false); mm->map_count++;
@@ -2239,9 +2279,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)) goto out_unlock; -vma_start_write (vma); }vma_iter_init (&vmi, mm,0 );
@@ -2540,8 +2579,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 +2591,14 @@ static int __mmap_new_vma(struct mmap_state *map, struct vm_area_struct **vmap,if (!vma)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; +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 +2629,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);vma_iter_store_new (vmi, vma); map->mm->map_count++;vma_link_file (vma, action->hide_from_rmap_until_complete);
@@ -2878,6 +2923,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;/* * Check against address space limits by the changed size
@@ -2910,24 +2956,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; + } }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; + }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; + } +if (vma_iter_store_gfp (vmi, vma,GFP_KERNEL )) { + err = -ENOMEM ; goto mas_store_fail; + } mm->map_count++;validate_mm (mm);
@@ -2942,10 +2997,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; }/**
@@ -3112,8 +3168,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;if (!vma_test (vma,VMA_GROWSUP_BIT ))return -EFAULT ;
@@ -3149,12 +3205,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; }/* 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);
@@ -3183,8 +3241,10 @@ int expand_upwards(struct vm_area_struct *vma, unsigned long address) } }anon_vma_unlock_write (vma->anon_vma); -vma_iter_free (&vmi);validate_mm (mm); +vma_lock_fail: +vma_prep_fail: +vma_iter_free (&vmi);return error; }#endif /* CONFIG_STACK_GROWSUP */
@@ -3197,8 +3257,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;if (!vma_test (vma,VMA_GROWSDOWN_BIT ))return -EFAULT ;
@@ -3228,12 +3288,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);
@@ -3263,8 +3325,10 @@ int expand_downwards(struct vm_area_struct *vma, unsigned long address) } }anon_vma_unlock_write (vma->anon_vma); -vma_iter_free (&vmi);validate_mm (mm); +vma_lock_fail: +vma_prep_fail: +vma_iter_free (&vmi);return error; }
mm/vma_exec.c +4 -2
@@ -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
Suren Baghdasaryan
reviewerPATCH v5 5/6 mm: use vma_start_write_killable() in process_vma_walk_lock()
In reply to Suren BaghdasaryanReplace 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 | 12 ++++++------ mm/mempolicy.c | 1 + mm/pagewalk.c | 22 +++++++++++++++------- 3 files changed, 22 insertions(+), 13 deletions(-)
fs/proc/task_mmu.c +6 -6
@@ -1774,15 +1774,15 @@ static ssize_t clear_refs_write(struct file *file, const char __user *buf,struct vm_area_struct *vma;enum clear_refs_types type;int itype; -int rv; +int err;if (count >sizeof (buffer) -1 ) count =sizeof (buffer) -1 ;if (copy_from_user (buffer, buf, count))return -EFAULT ; - rv =kstrtoint (strstrip (buffer),10 , &itype); -if (rv <0 ) -return rv; + err =kstrtoint (strstrip (buffer),10 , &itype); +if (err) +return err; type = (enum clear_refs_types )itype;if (type <CLEAR_REFS_ALL || type >=CLEAR_REFS_LAST )return -EINVAL ;
@@ -1824,7 +1824,7 @@ 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 (type ==CLEAR_REFS_SOFT_DIRTY ) {mmu_notifier_invalidate_range_end (&range);flush_tlb_mm (mm);
@@ -1837,7 +1837,7 @@ static ssize_t clear_refs_write(struct file *file, const char __user *buf, }put_task_struct (task); -return count; +return err ? : count; }const struct file_operations proc_clear_refs_operations = {
mm/mempolicy.c +1 -0
@@ -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 +15 -7
@@ -443,14 +443,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, -enum page_walk_lock walk_lock) +static 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 ;
@@ -462,6 +461,7 @@ static inline void process_vma_walk_lock(struct vm_area_struct *vma, break; }#endif +return 0 ; }/*
@@ -505,7 +505,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);
@@ -722,6 +724,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 ;
@@ -729,7 +732,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); }
@@ -752,6 +757,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 ;
@@ -759,7 +765,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
Suren Baghdasaryan
reviewerPATCH v5 6/6 KVM: PPC: use vma_start_write_killable() in kvmppc_memslot_page_merge()
In reply to Suren BaghdasaryanReplace vma_start_write() with vma_start_write_killable(), improving reaction time to the kill signal.
Replace vma_start_write() in kvmppc_memslot_page_merge().
--- arch/powerpc/kvm/book3s_hv_uvmem.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
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 ; + }/* 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,
Signature · 1 line
2.53.0.1018.g2bb0e51243-goog
Lorenzo Stoakes (Oracle)
maintainer-cc old email
(Gentle reminder to please send all new stuff to [email protected] - at some point I'm going to set a rule to ignore kernel mail sent to the old address so, if you need a response you should send to the new :)
Quoted 74 lines · Suren Baghdasaryan
Now that we have
vma_start_write_killable()we can replace most of thevma_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 withvma_assert_write_locked(), then callers of these functions should lock the vma themselves usingvma_start_write_killable()whenever possible.Changes since v4 [1]:
- added Reviewed-by, per Barry Song and Lorenzo Stoakes (wherever the code stayed the same)
- split patch 2 into 3 parts, per Lorenzo Stoakes
- convertedvma_start_write()inmseal_apply(), per Sashiko
- changedvma_start_write_killable()error handling inset_mempolicy_home_node(), per Lorenzo Stoakes
- added comment whymm->locked_vmis fine even when we exit early, per Sashiko
- moved vma locking beforevrm_calc_charge()inmove_vma(), per Sashiko and Lorenzo Stoakes
- setgive_up_on_oomon error invma_merge_existing_range()to propagate the error, per Lorenzo Stoakes
- movedvalidate_mm()out of the error path inexpand_upwards(), per Lorenzo Stoakes
- dropped the patch changing S390 error handling, per Sashiko and Lorenzo Stoakes
- reworked error handling inclear_refs_write(), per Lorenzo Stoakes
- uninlinedprocess_vma_walk_lock()while changing its return type, per Lorenzo Stoakes[1] https://lore.kernel.org/all/[email protected]/
Suren Baghdasaryan (6):
mm/vma: cleanup error handling path invma_expand()
mm: usevma_start_write_killable()in mm syscalls mm/khugepaged: usevma_start_write_killable()incollapse_huge_page()mm/vma: usevma_start_write_killable()in vma operations
mm: usevma_start_write_killable()inprocess_vma_walk_lock()
KVM: PPC: usevma_start_write_killable()in
kvmppc_memslot_page_merge()arch/powerpc/kvm/book3s_hv_uvmem.c | 5 +- fs/proc/task_mmu.c | 12 +-- 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 | 8 +- mm/mseal.c | 5 +- mm/pagewalk.c | 22 +++-- mm/vma.c | 146 +++++++++++++++++++++-------- mm/vma_exec.c | 6 +- 13 files changed, 190 insertions(+), 70 deletions(-)base-commit:
e53c9040ab1b738dd2c83b57558f141902caaf4f--
2.53.0.1018.g2bb0e51243-goog
Re: [PATCH v5 2/6] mm: use vma_start_write_killable() in mm syscalls
In reply to Suren BaghdasaryanQuoted 11 lines · Suren Baghdasaryan
Replace
vma_start_write()withvma_start_write_killable()in syscalls, improving reaction time to the kill signal.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.Suggested-by Matthew Wilcox <[email protected]>Signed-off-by Suren Baghdasaryan <[email protected]>
LGTM, so:
Quoted 213 lines
--- mm/madvise.c | 4 +++- mm/memory.c | 2 ++ mm/mempolicy.c | 11 +++++++++-- mm/mlock.c | 28 ++++++++++++++++++++++------ mm/mprotect.c | 5 ++++- mm/mremap.c | 8 +++++--- mm/mseal.c | 5 ++++- 7 files changed, 49 insertions(+), 14 deletions(-)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 ; + vma->flags = new_vma_flags;if (set_new_anon_name)return replace_anon_vma_name (vma, anon_name);mm/memory.c +2
-0@@ -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 +9
-2@@ -1784,7 +1784,8 @@ SYSCALL_DEFINE4(set_mempolicy_home_node, unsigned long, start, unsigned long, lereturn -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,19 @@ 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. +*/ + err =vma_start_write_killable (vma); +if (err) +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@@ -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;/* mm->locked_vm is fine as nr_pages == 0 */ /* 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@@ -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 +5
-3@@ -1348,6 +1348,11 @@ static unsigned long move_vma(struct vma_remap_struct *vrm)if (err)return err; +/* We don't want racing faults. */ + err =vma_start_write_killable (vrm->vma); +if (err) +return err; +/* * If accounted, determine the number of bytes the operation will * charge. @@ -1355,9 +1360,6 @@ static unsigned long move_vma(struct vma_remap_struct *vrm)if (!vrm_calc_charge (vrm))return -ENOMEM ; -/* We don't want racing faults. */ -vma_start_write (vrm->vma); -/* Perform copy step. */ err =copy_vma_and_data (vrm, &new_vma);/* mm/mseal.c +4
-2@@ -70,6 +70,7 @@ static int mseal_apply(struct mm_struct *mm,if (!vma_test (vma,VMA_SEALED_BIT )) {vma_flags_t vma_flags = vma->flags; +int err;vma_flags_set (&vma_flags,VMA_SEALED_BIT );@@ -77,7 +78,9 @@ static int mseal_apply(struct mm_struct *mm, curr_end, &vma_flags);if (IS_ERR (vma))return PTR_ERR (vma); -vma_start_write (vma); + err =vma_start_write_killable (vma); +if (err) +return err;vma_set_flags (vma,VMA_SEALED_BIT ); } --2.53.0.1018.g2bb0e51243-goog
Re: [PATCH v5 4/6] mm/vma: use vma_start_write_killable() in vma operations
In reply to Suren BaghdasaryanQuoted 4 lines · Suren Baghdasaryan
Replace
vma_start_write()withvma_start_write_killable(), improving reaction time to the kill signal.
Replacevma_start_write()calls when we operate on VMAs.
Quoted 8 lines
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.Suggested-by Matthew Wilcox <[email protected]>Signed-off-by Suren Baghdasaryan <[email protected]>
LGTM, so:
Quoted 38 lines
--- mm/vma.c | 130 +++++++++++++++++++++++++++++++++++++------------- mm/vma_exec.c | 6 ++- 2 files changed, 101 insertions(+), 35 deletions(-)mm/vma.c +11
-3@@ -524,6 +524,17 @@ __split_vma(struct vma_iterator *vmi, struct vm_area_struct *vma, new->vm_pgoff += ((addr - vma->vm_start) >>PAGE_SHIFT ); } +/* +* 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; + 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); -init_vma_prep (&vp, vma); vp.insert = new;vma_prepare (&vp);
mm/vma.c static __must_check struct vm_area_struct *vma_merge_existing_range( · line 913
Quoted 7 lines
mm/vma.c +3
-1@@ -900,12 +908,22 @@ 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) { +/* Ensure error propagates. */
Thanks! I will come back to this and clean all this up later.
Quoted 332 lines
mm/vma.c +83
-29+ vmg->give_up_on_oom = false; + goto abort; + }if (merge_right) {vma_flags_t next_sticky; -vma_start_write (next); + err =vma_start_write_killable (next); +if (err) { +/* Ensure error propagates. */ + vmg->give_up_on_oom = false; + goto abort; + } vmg->target = next; next_sticky =vma_flags_and_mask (&next->flags,VMA_STICKY_FLAGS );vma_flags_set_mask (&sticky_flags, next_sticky);@@ -914,7 +932,12 @@ 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) { +/* Ensure error propagates. */ + vmg->give_up_on_oom = false; + goto abort; + } vmg->target = prev; prev_sticky =vma_flags_and_mask (&prev->flags,VMA_STICKY_FLAGS );@@ -1170,10 +1193,12 @@ 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 err =0 ; +int err;mmap_assert_write_locked (vmg->mm); -vma_start_write (target); + err =vma_start_write_killable (target); +if (err) +return err; target_sticky =vma_flags_and_mask (&target->flags,VMA_STICKY_FLAGS );@@ -1201,6 +1226,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; err =dup_anon_vma (target, next, &anon_dup);if (err)return err;@@ -1214,7 +1246,6 @@ int vma_expand(struct vma_merge_struct *vmg)if (remove_next) {vma_flags_t next_sticky; -vma_start_write (next); vmg->__remove_next = true; next_sticky =vma_flags_and_mask (&next->flags,VMA_STICKY_FLAGS );@@ -1252,9 +1283,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 @@ -1263,8 +1299,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); -init_vma_prep (&vp, vma);vma_prepare (&vp);vma_adjust_trans_huge (vma, start, end,NULL );@@ -1453,7 +1487,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;mas_set (mas_detach, vms->vma_count++); error =mas_store_gfp (mas_detach, next,GFP_KERNEL );if (error)@@ -1848,12 +1884,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;vma_iter_config (&vmi, vma->vm_start, vma->vm_end);if (vma_iter_prealloc (&vmi, vma))return -ENOMEM ; -vma_start_write (vma);vma_iter_store_new (&vmi, vma);vma_link_file (vma,/* hold_rmap_lock= */ false); mm->map_count++;@@ -2239,9 +2279,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)) goto out_unlock; -vma_start_write (vma); }vma_iter_init (&vmi, mm,0 );@@ -2540,8 +2579,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 +2591,14 @@ static int __mmap_new_vma(struct mmap_state *map, struct vm_area_struct **vmap,if (!vma)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; +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 +2629,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);vma_iter_store_new (vmi, vma); map->mm->map_count++;vma_link_file (vma, action->hide_from_rmap_until_complete);@@ -2878,6 +2923,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;/* * Check against address space limits by the changed size @@ -2910,24 +2956,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; + } }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; + }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; + } +if (vma_iter_store_gfp (vmi, vma,GFP_KERNEL )) { + err = -ENOMEM ; goto mas_store_fail; + } mm->map_count++;validate_mm (mm);@@ -2942,10 +2997,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; }/** @@ -3112,8 +3168,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;if (!vma_test (vma,VMA_GROWSUP_BIT ))return -EFAULT ;@@ -3149,12 +3205,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; }/* 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);@@ -3183,8 +3241,10 @@ int expand_upwards(struct vm_area_struct *vma, unsigned long address) } }anon_vma_unlock_write (vma->anon_vma); -vma_iter_free (&vmi);validate_mm (mm); +vma_lock_fail: +vma_prep_fail: +vma_iter_free (&vmi);return error; }#endif /* CONFIG_STACK_GROWSUP */ @@ -3197,8 +3257,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;if (!vma_test (vma,VMA_GROWSDOWN_BIT ))return -EFAULT ;@@ -3228,12 +3288,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);@@ -3263,8 +3325,10 @@ int expand_downwards(struct vm_area_struct *vma, unsigned long address) } }anon_vma_unlock_write (vma->anon_vma); -vma_iter_free (&vmi);validate_mm (mm); +vma_lock_fail: +vma_prep_fail: +vma_iter_free (&vmi);return error; }mm/vma_exec.c +4
-3@@ -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 -- 2.53.0.1018.g2bb0e51243-goog
Re: [PATCH v5 5/6] mm: use vma_start_write_killable() in process_vma_walk_lock()
In reply to Suren BaghdasaryanQuoted 8 lines · Suren Baghdasaryan
Replace
vma_start_write()withvma_start_write_killable()whenprocess_vma_walk_lock()is used withPGWALK_WRLOCKoption. 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.Suggested-by Matthew Wilcox <[email protected]>Signed-off-by Suren Baghdasaryan <[email protected]>
LGTM, so:
Quoted 142 lines
--- fs/proc/task_mmu.c | 12 ++++++------ mm/mempolicy.c | 1 + mm/pagewalk.c | 22 +++++++++++++++------- 3 files changed, 22 insertions(+), 13 deletions(-)fs/proc/task_mmu.c +6
-6@@ -1774,15 +1774,15 @@ static ssize_t clear_refs_write(struct file *file, const char __user *buf,struct vm_area_struct *vma;enum clear_refs_types type;int itype; -int rv; +int err;if (count >sizeof (buffer) -1 ) count =sizeof (buffer) -1 ;if (copy_from_user (buffer, buf, count))return -EFAULT ; - rv =kstrtoint (strstrip (buffer),10 , &itype); -if (rv <0 ) -return rv; + err =kstrtoint (strstrip (buffer),10 , &itype); +if (err) +return err; type = (enum clear_refs_types )itype;if (type <CLEAR_REFS_ALL || type >=CLEAR_REFS_LAST )return -EINVAL ;@@ -1824,7 +1824,7 @@ 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 (type ==CLEAR_REFS_SOFT_DIRTY ) {mmu_notifier_invalidate_range_end (&range);flush_tlb_mm (mm);@@ -1837,7 +1837,7 @@ static ssize_t clear_refs_write(struct file *file, const char __user *buf, }put_task_struct (task); -return count; +return err ? : count; }const struct file_operations proc_clear_refs_operations = {mm/mempolicy.c +1
-0@@ -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 +15
-8@@ -443,14 +443,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, -enum page_walk_lock walk_lock) +static 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 ;@@ -462,6 +461,7 @@ static inline void process_vma_walk_lock(struct vm_area_struct *vma, break; }#endif +return 0 ; }/* @@ -505,7 +505,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);@@ -722,6 +724,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 ;@@ -729,7 +732,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); }@@ -752,6 +757,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 ;@@ -759,7 +765,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); } --2.53.0.1018.g2bb0e51243-goog
Suren Baghdasaryan
reviewerQuoted 6 lines · Lorenzo Stoakes (Oracle)
-cc old email
(Gentle reminder to please send all new stuff to [email protected] - at some point I'm going to set a rule to ignore kernel mail sent to the old address so, if you need a response you should send to the new :)
Ack, sorry about that and thanks for all the reviews, Lorenzo!
I noticed Sashiko's concerns at [1] and a number of them are false positives but I see at least one new claim which might be legit. I'll check if it holds water.
[1] https://sashiko.dev/#/patchset/20260326080836.695207-1-surenb%40google.com
Quoted 77 lines · Suren Baghdasaryan
Quoted 74 lines
Now that we have
vma_start_write_killable()we can replace most of thevma_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 withvma_assert_write_locked(), then callers of these functions should lock the vma themselves usingvma_start_write_killable()whenever possible.Changes since v4 [1]:
- added Reviewed-by, per Barry Song and Lorenzo Stoakes (wherever the code stayed the same)
- split patch 2 into 3 parts, per Lorenzo Stoakes
- convertedvma_start_write()inmseal_apply(), per Sashiko
- changedvma_start_write_killable()error handling inset_mempolicy_home_node(), per Lorenzo Stoakes
- added comment whymm->locked_vmis fine even when we exit early, per Sashiko
- moved vma locking beforevrm_calc_charge()inmove_vma(), per Sashiko and Lorenzo Stoakes
- setgive_up_on_oomon error invma_merge_existing_range()to propagate the error, per Lorenzo Stoakes
- movedvalidate_mm()out of the error path inexpand_upwards(), per Lorenzo Stoakes
- dropped the patch changing S390 error handling, per Sashiko and Lorenzo Stoakes
- reworked error handling inclear_refs_write(), per Lorenzo Stoakes
- uninlinedprocess_vma_walk_lock()while changing its return type, per Lorenzo Stoakes[1] https://lore.kernel.org/all/[email protected]/
Suren Baghdasaryan (6):
mm/vma: cleanup error handling path invma_expand()
mm: usevma_start_write_killable()in mm syscalls mm/khugepaged: usevma_start_write_killable()incollapse_huge_page()mm/vma: usevma_start_write_killable()in vma operations
mm: usevma_start_write_killable()inprocess_vma_walk_lock()
KVM: PPC: usevma_start_write_killable()in
kvmppc_memslot_page_merge()arch/powerpc/kvm/book3s_hv_uvmem.c | 5 +- fs/proc/task_mmu.c | 12 +-- 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 | 8 +- mm/mseal.c | 5 +- mm/pagewalk.c | 22 +++-- mm/vma.c | 146 +++++++++++++++++++++-------- mm/vma_exec.c | 6 +- 13 files changed, 190 insertions(+), 70 deletions(-)base-commit:
e53c9040ab1b738dd2c83b57558f141902caaf4f--
2.53.0.1018.g2bb0e51243-goog
Lorenzo Stoakes (Oracle)
maintainer+cc Roman
Quoted 8 lines · Suren Baghdasaryan
Quoted 6 lines · Lorenzo Stoakes (Oracle)
-cc old email
(Gentle reminder to please send all new stuff to [email protected] - at some point I'm going to set a rule to ignore kernel mail sent to the old address so, if you need a response you should send to the new :)
Quoted 8 lines
Ack, sorry about that and thanks for all the reviews, Lorenzo!
I noticed Sashiko's concerns at [1] and a number of them are false positives but I see at least one new claim which might be legit. I'll check if it holds water.
[1] https://sashiko.dev/#/patchset/20260326080836.695207-1-surenb%40google.com
Is worth letting Roman know which are valid and not, as that's useful information for refining the prompts etc.
Cheers! Lorenzo
Andrew Morton
maintainerQuoted 16 lines · On Thu, 26 Mar 2026 01:08:30 -0700 Suren Baghdasaryan <[email protected]> wrote:
Now that we have
vma_start_write_killable()we can replace most of thevma_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.
Quoted 5 lines
4. vm_flags_{set|mod|clear} require refactoring that involves moving
vma_start_write()out of these functions and replacing it withvma_assert_write_locked(), then callers of these functions should lock the vma themselves usingvma_start_write_killable()whenever possible.
Thanks, I added this to mm-new.
It doesn't appear to fix anything and it has no appreciable&measured performance benefits, so I just broke my own rule.
Weasel words: Lorenzo would like it in 7.0, unreviewed patches in mm-unstable&mm-new are down to 62 and I'm a sucker for nice patchsets.
Three of your patches lack review tags so now it's 65!
Andrew Morton
maintainerQuoted 1 line · On Fri, 27 Mar 2026 09:47:38 -0700 Andrew Morton <[email protected]>…
Three of your patches lack review tags so now it's 65!
Three is now zero, thanks Lorenzo.
Suren Baghdasaryan
reviewerQuoted 6 lines · Andrew Morton
Quoted 1 line · On Fri, 27 Mar 2026 09:47:38 -0700 Andrew Morton <[email protected]>…
Three of your patches lack review tags so now it's 65!
Three is now zero, thanks Lorenzo.
I just finished fixups for issues Sashiko flagged. They are pretty small but important. Testing them right now and running Sashiko locally. Once done I'll post v6 and will ask Lorenzo to re-review those specific changes. Sorry Lorenzo, I'll point out the specific changes I had to make and will try to make it easy for you. Thanks,
Suren.
Lorenzo Stoakes (Oracle)
maintainerQuoted 7 lines · Suren Baghdasaryan
Quoted 5 lines · Andrew Morton
Quoted 1 line · On Fri, 27 Mar 2026 09:47:38 -0700 Andrew Morton <[email protected]>…
Three of your patches lack review tags so now it's 65!
Quoted 7 lines
Quoted 1 line
Three is now zero, thanks Lorenzo.
I just finished fixups for issues Sashiko flagged. They are pretty small but important. Testing them right now and running Sashiko locally. Once done I'll post v6 and will ask Lorenzo to re-review those specific changes. Sorry Lorenzo, I'll point out the specific changes I had to make and will try to make it easy for you.
No worries :)
Could you send to [email protected] on the respin? Sorry to nag about it but my email more and more set up for that and makes my life a wee bit easier :>)
Quoted 2 lines
Thanks,
Suren.
Cheers, Lorenzo
Suren Baghdasaryan
reviewerQuoted 17 lines · Lorenzo Stoakes (Oracle)
Quoted 7 lines · Suren Baghdasaryan
Quoted 5 lines · Andrew Morton
Quoted 1 line · On Fri, 27 Mar 2026 09:47:38 -0700 Andrew Morton <[email protected]>…
Three of your patches lack review tags so now it's 65!
Quoted 7 lines
Quoted 1 line
Three is now zero, thanks Lorenzo.
I just finished fixups for issues Sashiko flagged. They are pretty small but important. Testing them right now and running Sashiko locally. Once done I'll post v6 and will ask Lorenzo to re-review those specific changes. Sorry Lorenzo, I'll point out the specific changes I had to make and will try to make it easy for you.
Quoted 4 lines
No worries :)
Could you send to [email protected] on the respin? Sorry to nag about it but my email more and more set up for that and makes my life a wee bit easier :>)
Absolutely. Thanks for the reminder.
Quoted 5 lines
Quoted 3 lines
Thanks,
Suren.Cheers, Lorenzo
Suren Baghdasaryan
reviewerQuoted 20 lines · Suren Baghdasaryan
Quoted 17 lines · Lorenzo Stoakes (Oracle)
Quoted 14 lines · Suren Baghdasaryan
Quoted 6 lines · Andrew Morton
Quoted 1 line · On Fri, 27 Mar 2026 09:47:38 -0700 Andrew Morton <[email protected]>…
Three of your patches lack review tags so now it's 65!
Three is now zero, thanks Lorenzo.
I just finished fixups for issues Sashiko flagged. They are pretty small but important. Testing them right now and running Sashiko locally. Once done I'll post v6 and will ask Lorenzo to re-review those specific changes. Sorry Lorenzo, I'll point out the specific changes I had to make and will try to make it easy for you.
Quoted 6 lines
Quoted 4 lines
No worries :)
Could you send to [email protected] on the respin? Sorry to nag about it but my email more and more set up for that and makes my life a wee bit easier :>)
Absolutely. Thanks for the reminder.
Doh! Just noticed git send-email doing this:
(body) Adding cc: Lorenzo Stoakes <[email protected]> from line 'Reviewed-by: Lorenzo Stoakes <[email protected]>'
But it should also land in your kernel.org mailbox. Just posted v6 at:
https://lore.kernel.org/all/[email protected]/
Lorenzo, please see the cover letter where I list per-patch changes. 3 patches not listed there were unchanged. That should help a bit with re-reviewing them. Thank you!
Quoted 6 lines
Quoted 6 lines
Quoted 4 lines
Thanks,
Suren.Cheers, Lorenzo