Use killable vma write locking in most places
Subsystems (13)
Suren Baghdasaryan
reviewerv3 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 and the last patch contains the changes associated with process_vma_walk_lock() error handling.
Changes since v2 [1]:
- rebased over mm-unstable, per Matthew Wilcox;
- removed mpol_rebind_mm() changes since the function operates on a remote mm and incomplete operation can leave unrelated process in an inconsistent state;
- moved vma_start_write_killable() inside set_mempolicy_home_node() to avoid locking extra vmas, per Liam R. Howlett
- moved vma_start_write_killable() inside __mmap_new_vma() to lock the vma right after it's allocation, per Liam R. Howlett
- introduced VMA_MERGE_ERROR_INTR to add EINTR handling for vma_modify()
- changed do_mbind() error handling for avoid EINTR overrides;
- changed migrate_to_node() error handling for avoid EINTR overrides;
- added EINTR handling in queue_pages_range();
- fixed clear_refs_write() error handling which previous verstion broke by skipping some of the cleanup logic;
[1] https://lore.kernel.org/all/[email protected]/
Suren Baghdasaryan (3):
mm/vma: cleanup error handling path in vma_expand()
mm: replace vma_start_write() with vma_start_write_killable()
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 | 22 +++-- mm/mlock.c | 21 +++-- mm/mprotect.c | 4 +- mm/mremap.c | 4 +- mm/pagewalk.c | 20 +++-- mm/vma.c | 127 ++++++++++++++++++++--------- mm/vma.h | 6 ++ mm/vma_exec.c | 6 +- 14 files changed, 167 insertions(+), 66 deletions(-)
base-commit: 6de23f81a5e08be8fbf5e8d7e9febc72a5b5f27f
Signature · 1 line
2.53.0.414.gf7e9f6c205-goog
Suren Baghdasaryan
reviewerPATCH v3 1/3 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.
No functional change intended.
--- mm/vma.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-)
mm/vma.c +8 -4
@@ -1186,12 +1186,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) +if (remove_next) { ret =dup_anon_vma (target, next, &anon_dup); -if (!ret && vmg->copied_from) +if (ret) +return ret; + } +if (vmg->copied_from) { ret =dup_anon_vma (target, vmg->copied_from, &anon_dup); -if (ret) -return ret; +if (ret) +return ret; + }if (remove_next) {vma_start_write (next);
Signature · 1 line
2.53.0.414.gf7e9f6c205-goog
Re: [PATCH v3 1/3] mm/vma: cleanup error handling path in vma_expand()
Quoted 9 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.No functional change intended.
Suggested-by Jann Horn <[email protected]>Signed-off-by Suren Baghdasaryan <[email protected]>
This looks the same as v2, so I'll try again ;)
Quoted 33 lines
--- mm/vma.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-)mm/vma.c +8
-4@@ -1186,12 +1186,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) +if (remove_next) { ret =dup_anon_vma (target, next, &anon_dup); -if (!ret && vmg->copied_from) +if (ret) +return ret; + } +if (vmg->copied_from) { ret =dup_anon_vma (target, vmg->copied_from, &anon_dup); -if (ret) -return ret; +if (ret) +return ret; + }if (remove_next) {vma_start_write (next);Signature · 1 line
2.53.0.414.gf7e9f6c205-goog
Suren Baghdasaryan
reviewerRe: [PATCH v3 1/3] mm/vma: cleanup error handling path in vma_expand()
Quoted 7 lines · Liam R. Howlett
Quoted 5 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.
Quoted 6 lines
Quoted 4 lines
No functional change intended.
Suggested-by Jann Horn <[email protected]>Signed-off-by Suren Baghdasaryan <[email protected]>This looks the same as v2, so I'll try again ;)
Sorry, missed adding it. So again, thank you very much!
Quoted 37 lines
Reviewed-by Liam R. Howlett <[email protected]>Quoted 33 lines
--- mm/vma.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-)mm/vma.c +8
-5@@ -1186,12 +1186,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) +if (remove_next) { ret =dup_anon_vma (target, next, &anon_dup); -if (!ret && vmg->copied_from) +if (ret) +return ret; + } +if (vmg->copied_from) { ret =dup_anon_vma (target, vmg->copied_from, &anon_dup); -if (ret) -return ret; +if (ret) +return ret; + }if (remove_next) {vma_start_write (next); --2.53.0.414.gf7e9f6c205-goog
Re: [PATCH v3 1/3] mm/vma: cleanup error handling path in vma_expand()
In reply to Suren BaghdasaryanQuoted 9 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.No functional change intended.
Suggested-by Jann Horn <[email protected]>Signed-off-by Suren Baghdasaryan <[email protected]>
LGTM, so:
Quoted 17 lines
--- mm/vma.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-)mm/vma.c +2
-2@@ -1186,12 +1186,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) +if (remove_next) { ret =dup_anon_vma (target, next, &anon_dup); -if (!ret && vmg->copied_from) +if (ret)
mm/vma.c int vma_expand(struct vma_merge_struct *vmg) · line 1199
Quoted 10 lines
mm/vma.c +6
-2+return ret; + } +if (vmg->copied_from) { ret =dup_anon_vma (target, vmg->copied_from, &anon_dup); -if (ret) -return ret; +if (ret) +return ret; + }
Thanks that is an improvement!
I was going to suggest declaring 'ret' in each block but that kinda adds noise so this is fine.
Maybe rename 'ret' to 'err' but not a big deal, this function could do with a little more cleanup too I think!
Quoted 6 lines
mm/vma.c +0
-1if (remove_next) {vma_start_write (next); --2.53.0.414.gf7e9f6c205-goog
Suren Baghdasaryan
reviewerRe: [PATCH v3 1/3] mm/vma: cleanup error handling path in vma_expand()
Quoted 7 lines · Lorenzo Stoakes
Quoted 5 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.
Quoted 8 lines
Quoted 4 lines
No functional change intended.
Suggested-by Jann Horn <[email protected]>Signed-off-by Suren Baghdasaryan <[email protected]>LGTM, so:
Reviewed-by Lorenzo Stoakes <[email protected]>
Thanks!
Quoted 29 lines
Quoted 18 lines
--- mm/vma.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-)mm/vma.c +2
-2@@ -1186,12 +1186,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) +if (remove_next) { ret =dup_anon_vma (target, next, &anon_dup); -if (!ret && vmg->copied_from) +if (ret)mm/vma.c int vma_expand(struct vma_merge_struct *vmg) · line 1199
Quoted 10 lines
mm/vma.c +6
-2+return ret; + } +if (vmg->copied_from) { ret =dup_anon_vma (target, vmg->copied_from, &anon_dup); -if (ret) -return ret; +if (ret) +return ret; + }
Quoted 7 lines
Thanks that is an improvement!
I was going to suggest declaring 'ret' in each block but that kinda adds noise so this is fine.
Maybe rename 'ret' to 'err' but not a big deal, this function could do with a little more cleanup too I think!
Sure, since I'll be posting a new version I'll do the rename too. Cheers!
Quoted 8 lines
Quoted 7 lines
mm/vma.c +0
-1if (remove_next) {vma_start_write (next); --2.53.0.414.gf7e9f6c205-goog
Suren Baghdasaryan
reviewerPATCH v3 2/3 mm: replace vma_start_write() with vma_start_write_killable()
In reply to Suren BaghdasaryanNow 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 cpusset 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.
--- arch/powerpc/kvm/book3s_hv_uvmem.c | 5 +- mm/khugepaged.c | 5 +- mm/madvise.c | 4 +- mm/memory.c | 2 + mm/mempolicy.c | 8 ++- mm/mlock.c | 21 +++++-- mm/mprotect.c | 4 +- mm/mremap.c | 4 +- mm/vma.c | 93 +++++++++++++++++++++--------- mm/vma_exec.c | 6 +- 10 files changed, 109 insertions(+), 43 deletions(-)
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,
mm/khugepaged.c +4 -1
@@ -1141,7 +1141,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;
mm/madvise.c +3 -1
@@ -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
@@ -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. +* 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 +6 -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,16 @@ SYSCALL_DEFINE4(set_mempolicy_home_node, unsigned long, start, unsigned long, le err = -EOPNOTSUPP ;break ; } +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 +15 -6
@@ -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,13 @@ 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 ) +return error; + prev = vma; + }cond_resched (); } out:
mm/mprotect.c +3 -1
@@ -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
@@ -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/vma.c +65 -28
247 lines not on this page · raw message
mm/vma_exec.c +4 -2
@@ -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
Signature · 1 line
2.53.0.414.gf7e9f6c205-goog
Re: [PATCH v3 2/3] mm: replace vma_start_write() with vma_start_write_killable()
Quoted 25 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 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 cpusset 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 withvma_assert_write_locked(), then callers of these functions should lock the vma themselves usingvma_start_write_killable()whenever possible.
Quoted 3 lines
Suggested-by Matthew Wilcox <[email protected]>Signed-off-by Suren Baghdasaryan <[email protected]>
Some nits below, but lgtm.
Quoted 298 lines
--- arch/powerpc/kvm/book3s_hv_uvmem.c | 5 +- mm/khugepaged.c | 5 +- mm/madvise.c | 4 +- mm/memory.c | 2 + mm/mempolicy.c | 8 ++- mm/mlock.c | 21 +++++-- mm/mprotect.c | 4 +- mm/mremap.c | 4 +- mm/vma.c | 93 +++++++++++++++++++++--------- mm/vma_exec.c | 6 +- 10 files changed, 109 insertions(+), 43 deletions(-)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,mm/khugepaged.c +4
-1@@ -1141,7 +1141,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;mm/madvise.c +3
-1@@ -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@@ -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. +* 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 +6
-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,16 @@ SYSCALL_DEFINE4(set_mempolicy_home_node, unsigned long, start, unsigned long, le err = -EOPNOTSUPP ;break ; } +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 +15
-6@@ -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,13 @@ 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 ) +return error; + prev = vma; + }cond_resched (); } out:mm/mprotect.c +3
-1@@ -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@@ -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/vma.c +25
-11@@ -530,6 +530,13 @@ __split_vma(struct vma_iterator *vmi, struct vm_area_struct *vma,if (err) goto out_free_vmi; + err =vma_start_write_killable (vma); +if (err) + goto out_free_mpol; + err =vma_start_write_killable (new); +if (err) + goto out_free_mpol; + err =anon_vma_clone (new, vma,VMA_OP_SPLIT );if (err) goto out_free_mpol;@@ -540,9 +547,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);@@ -895,16 +899,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) + goto abort;if (merge_right) { -vma_start_write (next); + err =vma_start_write_killable (next); +if (err) + goto abort; vmg->target = next; sticky_flags |= (next->vm_flags &VM_STICKY ); }if (merge_left) { -vma_start_write (prev); + err =vma_start_write_killable (prev); +if (err) + goto abort; vmg->target = prev; sticky_flags |= (prev->vm_flags &VM_STICKY ); }@@ -1155,10 +1165,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;@@ -1187,6 +1199,9 @@ int vma_expand(struct vma_merge_struct *vmg)* we don't need to account for vmg->give_up_on_mm here. */ if (remove_next) { + ret =vma_start_write_killable (next); +if (ret) +return ret; ret =dup_anon_vma (target, next, &anon_dup);if (ret)return ret;@@ -1197,10 +1212,8 @@ int vma_expand(struct vma_merge_struct *vmg)return ret; } -if (remove_next) { -vma_start_write (next); +if (remove_next) vmg->__remove_next = true; - }if (commit_merge (vmg)) goto nomem;@@ -1233,6 +1246,7 @@ 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));
mm/vma.c int vma_shrink(struct vma_iterator *vmi, struct vm_area_struct *vma, · line 1265
Quoted 9 lines
mm/vma.c +5
-1@@ -1244,7 +1258,11 @@ int vma_shrink(struct vma_iterator *vmi, struct vm_area_struct *vma,if (vma_iter_prealloc (vmi,NULL ))return -ENOMEM ; -vma_start_write (vma); + err =vma_start_write_killable (vma); +if (err) { +vma_iter_free (vmi); +return err; + }
Could avoid allocating here by reordering the lock, but this is fine.
Quoted 20 lines
mm/vma.c +4
-1init_vma_prep (&vp, vma);vma_prepare (&vp);@@ -1434,7 +1452,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)@@ -1828,12 +1848,17 @@ 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;
mm/vma.c static void vma_link_file(struct vm_area_struct *vma, bool hold_rmap_lock) · line 1861
Quoted 10 lines
mm/vma.c +5
-1vma_iter_config (&vmi, vma->vm_start, vma->vm_end);if (vma_iter_prealloc (&vmi, vma))return -ENOMEM ; -vma_start_write (vma); + err =vma_start_write_killable (vma); +if (err) { +vma_iter_free (&vmi); +return err; + }
Ditto here, ordering would mean no freeing.
Quoted 15 lines
mm/vma.c +1
-2vma_iter_store_new (&vmi, vma);vma_link_file (vma,/* hold_rmap_lock= */ false); mm->map_count++;@@ -2215,9 +2240,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 );
mm/vma.c static int __mmap_new_vma(struct mmap_state *map, struct vm_area_struct **vmap) · line 2553
Quoted 8 lines
mm/vma.c +5
-0@@ -2522,6 +2546,11 @@ static int __mmap_new_vma(struct mmap_state *map, struct vm_area_struct **vmap)if (!vma)return -ENOMEM ; +/* Lock the VMA since it is modified after insertion into VMA tree */ + error =vma_start_write_killable (vma); +if (error) + goto free_vma; +
There's no way this is going to fail, right?
Quoted 21 lines
mm/vma.c +1
-2vma_iter_config (vmi, map->addr, map->end);vma_set_range (vma, map->addr, map->end, map->pgoff);vm_flags_init (vma, map->vm_flags);@@ -2552,8 +2581,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, map->hold_file_rmap_lock);@@ -2864,6 +2891,7 @@ int do_brk_flags(struct vma_iterator *vmi, struct vm_area_struct *vma, unsigned long addr, unsigned long len, vm_flags_t vm_flags) { struct mm_struct *mm = current->mm; +int err = -ENOMEM ;/* * Check against address space limits by the changed size
mm/vma.c int do_brk_flags(struct vma_iterator *vmi, struct vm_area_struct *vma, · line 2942
Quoted 8 lines
mm/vma.c +4
-1@@ -2908,7 +2936,10 @@ int do_brk_flags(struct vma_iterator *vmi, struct vm_area_struct *vma,vma_set_range (vma, addr, addr + len, addr >>PAGE_SHIFT );vm_flags_init (vma, vm_flags); vma->vm_page_prot =vm_get_page_prot (vm_flags); -vma_start_write (vma); +if (vma_start_write_killable (vma)) { + err = -EINTR ; + goto mas_store_fail; + }
I'd rather have another label saying write lock failed. Really, this will never fail though (besides syzbot..)
Quoted 110 lines
mm/vma.c +15
-9if (vma_iter_store_gfp (vmi, vma,GFP_KERNEL )) goto mas_store_fail;@@ -2928,7 +2959,7 @@ int do_brk_flags(struct vma_iterator *vmi, struct vm_area_struct *vma,vm_area_free (vma); unacct_fail:vm_unacct_memory (len >>PAGE_SHIFT ); -return -ENOMEM ; +return err; }/** @@ -3089,7 +3120,7 @@ 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 ; +int error;VMA_ITERATOR (vmi, mm, vma->vm_start);if (!(vma->vm_flags &VM_GROWSUP ))@@ -3126,12 +3157,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 free; }/* Lock the VMA before expanding to prevent concurrent page faults */ -vma_start_write (vma); + error =vma_start_write_killable (vma); +if (error) + goto free;/* We update the anon VMA tree. */ anon_vma_lock_write (vma->anon_vma);@@ -3160,6 +3193,7 @@ int expand_upwards(struct vm_area_struct *vma, unsigned long address) } }anon_vma_unlock_write (vma->anon_vma); +free:vma_iter_free (&vmi);validate_mm (mm);return error;@@ -3174,7 +3208,7 @@ 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 ; +int error;VMA_ITERATOR (vmi, mm, vma->vm_start);if (!(vma->vm_flags &VM_GROWSDOWN ))@@ -3205,12 +3239,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 free; }/* Lock the VMA before expanding to prevent concurrent page faults */ -vma_start_write (vma); + error =vma_start_write_killable (vma); +if (error) + goto free;/* We update the anon VMA tree. */ anon_vma_lock_write (vma->anon_vma);@@ -3240,6 +3276,7 @@ int expand_downwards(struct vm_area_struct *vma, unsigned long address) } }anon_vma_unlock_write (vma->anon_vma); +free:vma_iter_free (&vmi);validate_mm (mm);return error;mm/vma_exec.c +4
-2@@ -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 Signature · 1 line
2.53.0.414.gf7e9f6c205-goog
Suren Baghdasaryan
reviewerRe: [PATCH v3 2/3] mm: replace vma_start_write() with vma_start_write_killable()
Quoted 27 lines · Liam R. Howlett
Quoted 25 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 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 cpusset 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 withvma_assert_write_locked(), then callers of these functions should lock the vma themselves usingvma_start_write_killable()whenever possible.
Quoted 7 lines
Quoted 3 lines
Suggested-by Matthew Wilcox <[email protected]>Signed-off-by Suren Baghdasaryan <[email protected]>Some nits below, but lgtm.
Reviewed-by Liam R. Howlett <[email protected]>
Thanks!
Quoted 302 lines
Quoted 302 lines
--- arch/powerpc/kvm/book3s_hv_uvmem.c | 5 +- mm/khugepaged.c | 5 +- mm/madvise.c | 4 +- mm/memory.c | 2 + mm/mempolicy.c | 8 ++- mm/mlock.c | 21 +++++-- mm/mprotect.c | 4 +- mm/mremap.c | 4 +- mm/vma.c | 93 +++++++++++++++++++++--------- mm/vma_exec.c | 6 +- 10 files changed, 109 insertions(+), 43 deletions(-)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,mm/khugepaged.c +4
-1@@ -1141,7 +1141,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;mm/madvise.c +3
-1@@ -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@@ -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. +* 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 +6
-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,16 @@ SYSCALL_DEFINE4(set_mempolicy_home_node, unsigned long, start, unsigned long, le err = -EOPNOTSUPP ;break ; } +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 +15
-6@@ -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,13 @@ 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 ) +return error; + prev = vma; + }cond_resched (); } out:mm/mprotect.c +3
-1@@ -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@@ -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/vma.c +25
-11@@ -530,6 +530,13 @@ __split_vma(struct vma_iterator *vmi, struct vm_area_struct *vma,if (err) goto out_free_vmi; + err =vma_start_write_killable (vma); +if (err) + goto out_free_mpol; + err =vma_start_write_killable (new); +if (err) + goto out_free_mpol; + err =anon_vma_clone (new, vma,VMA_OP_SPLIT );if (err) goto out_free_mpol;@@ -540,9 +547,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);@@ -895,16 +899,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) + goto abort;if (merge_right) { -vma_start_write (next); + err =vma_start_write_killable (next); +if (err) + goto abort; vmg->target = next; sticky_flags |= (next->vm_flags &VM_STICKY ); }if (merge_left) { -vma_start_write (prev); + err =vma_start_write_killable (prev); +if (err) + goto abort; vmg->target = prev; sticky_flags |= (prev->vm_flags &VM_STICKY ); }@@ -1155,10 +1165,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;@@ -1187,6 +1199,9 @@ int vma_expand(struct vma_merge_struct *vmg)* we don't need to account for vmg->give_up_on_mm here. */ if (remove_next) { + ret =vma_start_write_killable (next); +if (ret) +return ret; ret =dup_anon_vma (target, next, &anon_dup);if (ret)return ret;@@ -1197,10 +1212,8 @@ int vma_expand(struct vma_merge_struct *vmg)return ret; } -if (remove_next) { -vma_start_write (next); +if (remove_next) vmg->__remove_next = true; - }if (commit_merge (vmg)) goto nomem;@@ -1233,6 +1246,7 @@ 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));@@ -1244,7 +1258,11 @@ int vma_shrink(struct vma_iterator *vmi, struct vm_area_struct *vma,if (vma_iter_prealloc (vmi,NULL ))return -ENOMEM ;
Quoted 8 lines
mm/vma.c int vma_shrink(struct vma_iterator *vmi, struct vm_area_struct *vma, · line 1265
Quoted 6 lines
mm/vma.c +5
-1-vma_start_write (vma); + err =vma_start_write_killable (vma); +if (err) { +vma_iter_free (vmi); +return err; + }Could avoid allocating here by reordering the lock, but this is fine.
Ack. I'll move it before vma_iter_prealloc().
Quoted 25 lines
Quoted 25 lines
mm/vma.c +4
-1init_vma_prep (&vp, vma);vma_prepare (&vp);@@ -1434,7 +1452,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)@@ -1828,12 +1848,17 @@ 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;vma_iter_config (&vmi, vma->vm_start, vma->vm_end);if (vma_iter_prealloc (&vmi, vma))return -ENOMEM ;
Quoted 8 lines
mm/vma.c static void vma_link_file(struct vm_area_struct *vma, bool hold_rmap_lock) · line 1861
Quoted 6 lines
mm/vma.c +5
-1-vma_start_write (vma); + err =vma_start_write_killable (vma); +if (err) { +vma_iter_free (&vmi); +return err; + }Ditto here, ordering would mean no freeing.
Ack. Will move.
Quoted 19 lines
Quoted 19 lines
mm/vma.c +1
-2vma_iter_store_new (&vmi, vma);vma_link_file (vma,/* hold_rmap_lock= */ false); mm->map_count++;@@ -2215,9 +2240,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 );@@ -2522,6 +2546,11 @@ static int __mmap_new_vma(struct mmap_state *map, struct vm_area_struct **vmap)if (!vma)return -ENOMEM ;
Quoted 7 lines
mm/vma.c static int __mmap_new_vma(struct mmap_state *map, struct vm_area_struct **vmap) · line 2553
Quoted 5 lines
mm/vma.c +5
-0+/* Lock the VMA since it is modified after insertion into VMA tree */ + error =vma_start_write_killable (vma); +if (error) + goto free_vma; +There's no way this is going to fail, right?
You are technically correct but this is only true because of how vma_start_write_killable() is implemented, so it's a technical detail of the implementation, which might change in the future... To clarify, __vma_start_exclude_readers() always succeeds if the vma is detached (which is the case here) because in that case it bails out before calling rcuwait_wait_event().
Quoted 19 lines
Quoted 19 lines
mm/vma.c +1
-2vma_iter_config (vmi, map->addr, map->end);vma_set_range (vma, map->addr, map->end, map->pgoff);vm_flags_init (vma, map->vm_flags);@@ -2552,8 +2581,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, map->hold_file_rmap_lock);@@ -2864,6 +2891,7 @@ int do_brk_flags(struct vma_iterator *vmi, struct vm_area_struct *vma, unsigned long addr, unsigned long len, vm_flags_t vm_flags) { struct mm_struct *mm = current->mm; +int err = -ENOMEM ;
Quoted 14 lines
mm/vma.c int do_brk_flags(struct vma_iterator *vmi, struct vm_area_struct *vma, · line 2942
Quoted 11 lines
mm/vma.c +4
-1/* * Check against address space limits by the changed size @@ -2908,7 +2936,10 @@ int do_brk_flags(struct vma_iterator *vmi, struct vm_area_struct *vma,vma_set_range (vma, addr, addr + len, addr >>PAGE_SHIFT );vm_flags_init (vma, vm_flags); vma->vm_page_prot =vm_get_page_prot (vm_flags); -vma_start_write (vma); +if (vma_start_write_killable (vma)) { + err = -EINTR ; + goto mas_store_fail; + }I'd rather have another label saying write lock failed. Really, this will never fail though (besides syzbot..)
Sure, I'll add another label.
Quoted 112 lines
Quoted 111 lines
mm/vma.c +15
-9if (vma_iter_store_gfp (vmi, vma,GFP_KERNEL )) goto mas_store_fail;@@ -2928,7 +2959,7 @@ int do_brk_flags(struct vma_iterator *vmi, struct vm_area_struct *vma,vm_area_free (vma); unacct_fail:vm_unacct_memory (len >>PAGE_SHIFT ); -return -ENOMEM ; +return err; }/** @@ -3089,7 +3120,7 @@ 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 ; +int error;VMA_ITERATOR (vmi, mm, vma->vm_start);if (!(vma->vm_flags &VM_GROWSUP ))@@ -3126,12 +3157,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 free; }/* Lock the VMA before expanding to prevent concurrent page faults */ -vma_start_write (vma); + error =vma_start_write_killable (vma); +if (error) + goto free;/* We update the anon VMA tree. */ anon_vma_lock_write (vma->anon_vma);@@ -3160,6 +3193,7 @@ int expand_upwards(struct vm_area_struct *vma, unsigned long address) } }anon_vma_unlock_write (vma->anon_vma); +free:vma_iter_free (&vmi);validate_mm (mm);return error;@@ -3174,7 +3208,7 @@ 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 ; +int error;VMA_ITERATOR (vmi, mm, vma->vm_start);if (!(vma->vm_flags &VM_GROWSDOWN ))@@ -3205,12 +3239,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 free; }/* Lock the VMA before expanding to prevent concurrent page faults */ -vma_start_write (vma); + error =vma_start_write_killable (vma); +if (error) + goto free;/* We update the anon VMA tree. */ anon_vma_lock_write (vma->anon_vma);@@ -3240,6 +3276,7 @@ int expand_downwards(struct vm_area_struct *vma, unsigned long address) } }anon_vma_unlock_write (vma->anon_vma); +free:vma_iter_free (&vmi);validate_mm (mm);return error;mm/vma_exec.c +4
-3@@ -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 -- 2.53.0.414.gf7e9f6c205-goog
Re: [PATCH v3 2/3] mm: replace vma_start_write() with vma_start_write_killable()
In reply to Suren BaghdasaryanQuoted 6 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 patch:
Quoted 8 lines
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.
Surprise surprise :))
Quoted 2 lines
4.
mpol_rebind_mm()which is used by cpusset controller for migrations
Incredibly nitty but cpusset -> cpuset?
Quoted 8 lines
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 withvma_assert_write_locked(), then callers of these functions should lock the vma themselves usingvma_start_write_killable()whenever possible.
This should be dealt with by my ongoing mmap_prepare, vma flags work.
Quoted 4 lines
Suggested-by Matthew Wilcox <[email protected]>Signed-off-by Suren Baghdasaryan <[email protected]>
Overall I'm a little concerned about whether callers can handle -EINTR in all cases, have you checked? Might we cause some weirdness in userspace if a syscall suddenly returns -EINTR when before it didn't?
Also maybe we should update the manpages to reflect this, as semi-usless as the 'possible error codes' sections are...
Quoted 78 lines
--- arch/powerpc/kvm/book3s_hv_uvmem.c | 5 +- mm/khugepaged.c | 5 +- mm/madvise.c | 4 +- mm/memory.c | 2 + mm/mempolicy.c | 8 ++- mm/mlock.c | 21 +++++-- mm/mprotect.c | 4 +- mm/mremap.c | 4 +- mm/vma.c | 93 +++++++++++++++++++++--------- mm/vma_exec.c | 6 +- 10 files changed, 109 insertions(+), 43 deletions(-)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,mm/khugepaged.c +4
-1@@ -1141,7 +1141,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;mm/madvise.c +3
-1@@ -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@@ -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. +* 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 +0
-0
mm/mempolicy.c SYSCALL_DEFINE4(set_mempolicy_home_node, unsigned long, start, unsigned long, le · line 1788
Quoted 6 lines
mm/mempolicy.c +2
-1@@ -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 ;
Hmm mmap write lock as well now :) I guess it makes sense here, esp given mmap write lock part of VMA write lock.
Quoted 8 lines
mm/mempolicy.c +1
-0prev =vma_prev (&vmi);for_each_vma_range (vmi, vma, end) {/* @@ -1801,13 +1802,16 @@ SYSCALL_DEFINE4(set_mempolicy_home_node, unsigned long, start, unsigned long, le err = -EOPNOTSUPP ;break ; } +if (vma_start_write_killable (vma)) {
mm/mempolicy.c SYSCALL_DEFINE4(set_mempolicy_home_node, unsigned long, start, unsigned long, le · line 1810
Quoted 10 lines
mm/mempolicy.c +3
-1+ err = -EINTR ; +break ; + } new =mpol_dup (old);if (IS_ERR (new)) { err =PTR_ERR (new);break ; } -vma_start_write (vma);
Are we ok with moving this to before mpol_dup()? Does this matter? Confused as to why you moved this up?
mm/mlock.c static int mlock_pte_range(pmd_t *pmd, unsigned long addr, · line 422
Quoted 11 lines
mm/mempolicy.c +0
-0new->home_node = home_node; err =mbind_range (&vmi, vma, &prev, start, end, new);mpol_put (new);mm/mlock.c +0
-0@@ -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. */
You should update the comment to reflect this possible return value.
Quoted 25 lines
mm/mlock.c +5
-2-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 ; }/*
mm/mlock.c static int mlock_fixup(struct vma_iterator *vmi, struct vm_area_struct *vma, · line 506
Quoted 3 lines
mm/mlock.c +0
-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 */
I'd move this comment down to the vm_flags_reset() line as it's not applicable if we fail to get the lock.
Quoted 12 lines
mm/mlock.c +4
-2-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;
mm/mlock.c static int apply_mlockall_flags(int flags) · line 741
Quoted 6 lines
mm/mlock.c +1
-2@@ -733,9 +738,13 @@ 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. */
Well, except you're not fixing it up on -EINTR? This comment should be redone.
But I wonder if this is correct? We are ignoring all other errors that interrupted the operation, so why are we special casing -EINTR?
Quoted 14 lines
mm/mlock.c +5
-0+if (error) { +if (error == -EINTR ) +return error; + prev = vma; + }cond_resched (); } out:mm/mprotect.c +0
-0
mm/mprotect.c mprotect_fixup(struct vma_iterator *vmi, struct mmu_gather *tlb, · line 769
Quoted 6 lines
mm/mprotect.c +2
-1@@ -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 )
Weird inconstency here, this should be if (error).
Quoted 38 lines
mm/mprotect.c +1
-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@@ -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/vma.c +7
-0@@ -530,6 +530,13 @@ __split_vma(struct vma_iterator *vmi, struct vm_area_struct *vma,if (err) goto out_free_vmi; + err =vma_start_write_killable (vma); +if (err) + goto out_free_mpol; + err =vma_start_write_killable (new); +if (err) + goto out_free_mpol; + err =anon_vma_clone (new, vma,VMA_OP_SPLIT );if (err) goto out_free_mpol;
mm/vma.c __split_vma(struct vma_iterator *vmi, struct vm_area_struct *vma, · line 545
Quoted 6 lines
mm/vma.c +0
-3@@ -540,9 +547,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); -
Again you're changing ordering seemingly arbitrarily. I think this is actually a more problematic case as you're now invoking vm_ops->open() with a VMA write lock held.
So I think you should keep the existing position.
Quoted 55 lines
mm/vma.c +16
-5init_vma_prep (&vp, vma); vp.insert = new;vma_prepare (&vp);@@ -895,16 +899,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) + goto abort;if (merge_right) { -vma_start_write (next); + err =vma_start_write_killable (next); +if (err) + goto abort; vmg->target = next; sticky_flags |= (next->vm_flags &VM_STICKY ); }if (merge_left) { -vma_start_write (prev); + err =vma_start_write_killable (prev); +if (err) + goto abort; vmg->target = prev; sticky_flags |= (prev->vm_flags &VM_STICKY ); }@@ -1155,10 +1165,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;@@ -1187,6 +1199,9 @@ int vma_expand(struct vma_merge_struct *vmg)* we don't need to account for vmg->give_up_on_mm here. */ if (remove_next) { + ret =vma_start_write_killable (next); +if (ret) +return ret; ret =dup_anon_vma (target, next, &anon_dup);if (ret)return ret;
mm/vma.c int vma_expand(struct vma_merge_struct *vmg) · line 1203
Quoted 8 lines
mm/vma.c +1
-3@@ -1197,10 +1212,8 @@ int vma_expand(struct vma_merge_struct *vmg)return ret; } -if (remove_next) { -vma_start_write (next); +if (remove_next) vmg->__remove_next = true; - }
Hmm you're moving the ordering of things around again :) You should have made this change as part of patch 1 anyway first so this patch wouldn't have a refactoring in it too.
Top of rmap.c suggests you _can_ take the VMA write lock prior to trying the dup but I'm just not sure why you'd want to switch these around in this patch?
Can we try to keep original ordering unless there's a really good reason not to?
Quoted 66 lines
mm/vma.c +16
-5if (commit_merge (vmg)) goto nomem;@@ -1233,6 +1246,7 @@ 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));@@ -1244,7 +1258,11 @@ int vma_shrink(struct vma_iterator *vmi, struct vm_area_struct *vma,if (vma_iter_prealloc (vmi,NULL ))return -ENOMEM ; -vma_start_write (vma); + err =vma_start_write_killable (vma); +if (err) { +vma_iter_free (vmi); +return err; + }init_vma_prep (&vp, vma);vma_prepare (&vp);@@ -1434,7 +1452,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)@@ -1828,12 +1848,17 @@ 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;vma_iter_config (&vmi, vma->vm_start, vma->vm_end);if (vma_iter_prealloc (&vmi, vma))return -ENOMEM ; -vma_start_write (vma); + err =vma_start_write_killable (vma); +if (err) { +vma_iter_free (&vmi); +return err; + }vma_iter_store_new (&vmi, vma);vma_link_file (vma,/* hold_rmap_lock= */ false); mm->map_count++;@@ -2215,9 +2240,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 );
mm/vma.c static int __mmap_new_vma(struct mmap_state *map, struct vm_area_struct **vmap) · line 2553
Quoted 8 lines
mm/vma.c +5
-0@@ -2522,6 +2546,11 @@ static int __mmap_new_vma(struct mmap_state *map, struct vm_area_struct **vmap)if (!vma)return -ENOMEM ; +/* Lock the VMA since it is modified after insertion into VMA tree */ + error =vma_start_write_killable (vma); +if (error) + goto free_vma; +
You're doing it again :)
Can we please keep the lock acquisition at the point it is in unless there's a really good reason not to.
And if there is a good reason, please do it in another commit prior to the massive 'change everything' one so it's more easily reviewable :)
Quoted 13 lines
mm/vma.c +0
-2vma_iter_config (vmi, map->addr, map->end);vma_set_range (vma, map->addr, map->end, map->pgoff);vm_flags_init (vma, map->vm_flags);@@ -2552,8 +2581,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, map->hold_file_rmap_lock);
mm/vma.c int do_brk_flags(struct vma_iterator *vmi, struct vm_area_struct *vma, · line 2894
Quoted 4 lines
mm/vma.c +1
-0@@ -2864,6 +2891,7 @@ int do_brk_flags(struct vma_iterator *vmi, struct vm_area_struct *vma, unsigned long addr, unsigned long len, vm_flags_t vm_flags) { struct mm_struct *mm = current->mm; +int err = -ENOMEM ;
I hate this 'default error code' pattern, it's a code smell. Please update everything that jumps to the failure case to set err, and leave this uninitialised.
We've had real bugs come out of this before!
Quoted 52 lines
mm/vma.c +11
-6/* * Check against address space limits by the changed size @@ -2908,7 +2936,10 @@ int do_brk_flags(struct vma_iterator *vmi, struct vm_area_struct *vma,vma_set_range (vma, addr, addr + len, addr >>PAGE_SHIFT );vm_flags_init (vma, vm_flags); vma->vm_page_prot =vm_get_page_prot (vm_flags); -vma_start_write (vma); +if (vma_start_write_killable (vma)) { + err = -EINTR ; + goto mas_store_fail; + }if (vma_iter_store_gfp (vmi, vma,GFP_KERNEL )) goto mas_store_fail;@@ -2928,7 +2959,7 @@ int do_brk_flags(struct vma_iterator *vmi, struct vm_area_struct *vma,vm_area_free (vma); unacct_fail:vm_unacct_memory (len >>PAGE_SHIFT ); -return -ENOMEM ; +return err; }/** @@ -3089,7 +3120,7 @@ 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 ; +int error;VMA_ITERATOR (vmi, mm, vma->vm_start);if (!(vma->vm_flags &VM_GROWSUP ))@@ -3126,12 +3157,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 free; }/* Lock the VMA before expanding to prevent concurrent page faults */ -vma_start_write (vma); + error =vma_start_write_killable (vma); +if (error) + goto free;/* We update the anon VMA tree. */ anon_vma_lock_write (vma->anon_vma);
mm/vma.c int expand_upwards(struct vm_area_struct *vma, unsigned long address) · line 3196
Quoted 4 lines
mm/vma.c +1
-0@@ -3160,6 +3193,7 @@ int expand_upwards(struct vm_area_struct *vma, unsigned long address) } }anon_vma_unlock_write (vma->anon_vma); +free:
Nitty, but this kinda sucks as a label name, generally when the error label contains 'free' it is free_xxx where 'xxx' is some specific thing.
So somethiing like 'fail' would be good.
Quoted 31 lines
mm/vma.c +6
-4vma_iter_free (&vmi);validate_mm (mm);return error;@@ -3174,7 +3208,7 @@ 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 ; +int error;VMA_ITERATOR (vmi, mm, vma->vm_start);if (!(vma->vm_flags &VM_GROWSDOWN ))@@ -3205,12 +3239,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 free; }/* Lock the VMA before expanding to prevent concurrent page faults */ -vma_start_write (vma); + error =vma_start_write_killable (vma); +if (error) + goto free;/* 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 3279
Quoted 4 lines
mm/vma.c +1
-0@@ -3240,6 +3276,7 @@ int expand_downwards(struct vm_area_struct *vma, unsigned long address) } }anon_vma_unlock_write (vma->anon_vma); +free:
Obviously same comment her :)
Quoted 16 lines
mm/vma.c +0
-0vma_iter_free (&vmi);validate_mm (mm);return error;mm/vma_exec.c +1
-0@@ -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);
mm/vma_exec.c int relocate_vma_down(struct vm_area_struct *vma, unsigned long shift) · line 61
Quoted 8 lines
mm/vma_exec.c +3
-2@@ -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;
Hmm. But before we were filtering the errors and now we're not... I guess not an issue as before it could _only_ return -ENOMEM, but again, are we sure all callers are fine with -EINTR I guess :)
Quoted 6 lines
mm/vma_exec.c +0
-1/* * move the page tables downwards, on failure we rely on -- 2.53.0.414.gf7e9f6c205-goog
Cheers, Lorenzo
Suren Baghdasaryan
reviewerRe: [PATCH v3 2/3] mm: replace vma_start_write() with vma_start_write_killable()
Quoted 17 lines · Lorenzo Stoakes
Quoted 6 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 patch:
Quoted 8 lines
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.
Quoted 6 lines
Surprise surprise :))
Quoted 2 lines
4.
mpol_rebind_mm()which is used by cpusset controller for migrationsIncredibly nitty but cpusset -> cpuset?
Ack.
Quoted 11 lines
Quoted 9 lines
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 withvma_assert_write_locked(), then callers of these functions should lock the vma themselves usingvma_start_write_killable()whenever possible.This should be dealt with by my ongoing
mmap_prepare, vma flags work.
That would be great! It makes it much simpler once you are done with that refactoring.
Quoted 9 lines
Quoted 5 lines
Suggested-by Matthew Wilcox <[email protected]>Signed-off-by Suren Baghdasaryan <[email protected]>Overall I'm a little concerned about whether callers can handle
-EINTRin all cases, have you checked? Might we cause some weirdness in userspace if a syscall suddenly returns-EINTRwhen before it didn't?
I did check the kernel users and put the patchset through AI reviews. I haven't checked if any of the affected syscalls do not advertise -EINTR as a possible error. Adding that to my todo list for the next respin.
Quoted 3 lines
Also maybe we should update the manpages to reflect this, as semi-usless as the 'possible error codes' sections are...
Ok, I'll check which syscalls need to be updated and will note that in cover letter. Once the patchset is accepted I'll update the manpages for them.
Quoted 80 lines
Quoted 80 lines
--- arch/powerpc/kvm/book3s_hv_uvmem.c | 5 +- mm/khugepaged.c | 5 +- mm/madvise.c | 4 +- mm/memory.c | 2 + mm/mempolicy.c | 8 ++- mm/mlock.c | 21 +++++-- mm/mprotect.c | 4 +- mm/mremap.c | 4 +- mm/vma.c | 93 +++++++++++++++++++++--------- mm/vma_exec.c | 6 +- 10 files changed, 109 insertions(+), 43 deletions(-)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,mm/khugepaged.c +4
-1@@ -1141,7 +1141,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;mm/madvise.c +3
-1@@ -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@@ -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. +* 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 +0
-0@@ -1784,7 +1784,8 @@ SYSCALL_DEFINE4(set_mempolicy_home_node, unsigned long, start, unsigned long, lereturn -EINVAL ;
Quoted 8 lines
mm/mempolicy.c SYSCALL_DEFINE4(set_mempolicy_home_node, unsigned long, start, unsigned long, le · line 1788
Quoted 5 lines
mm/mempolicy.c +2
-1if (end == start)return 0 ; -mmap_write_lock (mm); +if (mmap_write_lock_killable (mm)) +return -EINTR ;Hmm mmap write lock as well now :) I guess it makes sense here, esp given mmap write lock part of VMA write lock.
Yeah, I thought while we are at it we can make this part a bit better too...
Quoted 19 lines
Quoted 19 lines
mm/mempolicy.c +4
-0prev =vma_prev (&vmi);for_each_vma_range (vmi, vma, end) {/* @@ -1801,13 +1802,16 @@ SYSCALL_DEFINE4(set_mempolicy_home_node, unsigned long, start, unsigned long, le err = -EOPNOTSUPP ;break ; } +if (vma_start_write_killable (vma)) { + err = -EINTR ; +break ; + } new =mpol_dup (old);if (IS_ERR (new)) { err =PTR_ERR (new);break ; }
Quoted 4 lines
mm/mempolicy.c SYSCALL_DEFINE4(set_mempolicy_home_node, unsigned long, start, unsigned long, le · line 1810
Quoted 1 line
mm/mempolicy.c +0
-1-vma_start_write (vma);Are we ok with moving this to before
mpol_dup()? Does this matter? Confused as to why you moved this up?
I thought if locking fails, it would be better to check that earlier before allocating a new mempolicy. That seems to be safe, but thinking about this now, if allocation goes into direct reclaim and causes the lock to be held for longer then that might not be such a hot idea...
Quoted 14 lines
mm/mlock.c static int mlock_pte_range(pmd_t *pmd, unsigned long addr, · line 422
Quoted 12 lines
mm/mempolicy.c +0
-0new->home_node = home_node; err =mbind_range (&vmi, vma, &prev, start, end, new);mpol_put (new);mm/mlock.c +0
-0@@ -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. */ You should update the comment to reflect this possible return value.
Ack.
Quoted 24 lines
Quoted 24 lines
mm/mlock.c +5
-2-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 ; }
Quoted 8 lines
mm/mlock.c static int mlock_fixup(struct vma_iterator *vmi, struct vm_area_struct *vma, · line 506
Quoted 5 lines
mm/mlock.c +0
-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 */ I'd move this comment down to the
vm_flags_reset()line as it's not applicable if we fail to get the lock.
Ack.
Quoted 15 lines
Quoted 15 lines
mm/mlock.c +4
-2-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,13 @@ static int apply_mlockall_flags(int flags)
Quoted 7 lines
mm/mlock.c static int apply_mlockall_flags(int flags) · line 741
Quoted 5 lines
mm/mlock.c +1
-2error =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. */ Well, except you're not fixing it up on
-EINTR? This comment should be redone.
Hmm, should we fixup if the process is terminating? Does it matter at this point?
Quoted 3 lines
But I wonder if this is correct? We are ignoring all other errors that interrupted the operation, so why are we special casing
-EINTR?
Well, -EINTR means all the work you are doing here is useless because the process is about to go away. So, in that respect I think it's different from other errors.
Quoted 15 lines
Quoted 15 lines
mm/mlock.c +5
-0+if (error) { +if (error == -EINTR ) +return error; + prev = vma; + }cond_resched (); } out:mm/mprotect.c +0
-0@@ -765,7 +765,9 @@ mprotect_fixup(struct vma_iterator *vmi, struct mmu_gather *tlb,
Quoted 8 lines
mm/mprotect.c mprotect_fixup(struct vma_iterator *vmi, struct mmu_gather *tlb, · line 769
Quoted 6 lines
mm/mprotect.c +2
-1* 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 )Weird inconstency here, this should be if (error).
Ack.
Quoted 42 lines
Quoted 42 lines
mm/mprotect.c +1
-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@@ -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/vma.c +7
-0@@ -530,6 +530,13 @@ __split_vma(struct vma_iterator *vmi, struct vm_area_struct *vma,if (err) goto out_free_vmi; + err =vma_start_write_killable (vma); +if (err) + goto out_free_mpol; + err =vma_start_write_killable (new); +if (err) + goto out_free_mpol; + err =anon_vma_clone (new, vma,VMA_OP_SPLIT );if (err) goto out_free_mpol;@@ -540,9 +547,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);
Quoted 5 lines
mm/vma.c __split_vma(struct vma_iterator *vmi, struct vm_area_struct *vma, · line 545
Quoted 3 lines
mm/vma.c +0
-3-vma_start_write (vma); -vma_start_write (new); -Again you're changing ordering seemingly arbitrarily.
I moved it up to avoid undoing all the work above (vm_ops->open, vma_dup_policy, vma_iter_prealloc, vm_area_dup,..)
Quoted 3 lines
I think this is actually a
more problematic case as you're now invokingvm_ops->open()with a VMA write lock held.
Are you concerned about potential increased duration of the vma lock being held or that open() might try to take that lock itself (which is not a problem because vma write locks are reentrant)? Maybe some other concern I'm missing?
Quoted 2 lines
So I think you should keep the existing position.
If we do that then we would have to undo a bunch of operations. I'm fine adding that if there are reasons to avoid this move.
Quoted 69 lines
Quoted 56 lines
mm/vma.c +16
-5init_vma_prep (&vp, vma); vp.insert = new;vma_prepare (&vp);@@ -895,16 +899,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) + goto abort;if (merge_right) { -vma_start_write (next); + err =vma_start_write_killable (next); +if (err) + goto abort; vmg->target = next; sticky_flags |= (next->vm_flags &VM_STICKY ); }if (merge_left) { -vma_start_write (prev); + err =vma_start_write_killable (prev); +if (err) + goto abort; vmg->target = prev; sticky_flags |= (prev->vm_flags &VM_STICKY ); }@@ -1155,10 +1165,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;@@ -1187,6 +1199,9 @@ int vma_expand(struct vma_merge_struct *vmg)* we don't need to account for vmg->give_up_on_mm here. */ if (remove_next) { + ret =vma_start_write_killable (next); +if (ret) +return ret; ret =dup_anon_vma (target, next, &anon_dup);if (ret)return ret;mm/vma.c int vma_expand(struct vma_merge_struct *vmg) · line 1203
Quoted 8 lines
mm/vma.c +1
-3@@ -1197,10 +1212,8 @@ int vma_expand(struct vma_merge_struct *vmg)return ret; } -if (remove_next) { -vma_start_write (next); +if (remove_next) vmg->__remove_next = true; - }Hmm you're moving the ordering of things around again :) You should have made this change as part of patch 1 anyway first so this patch wouldn't have a refactoring in it too.
Quoted 4 lines
Top of
rmap.csuggests you _can_ take the VMA write lock prior to trying the dup but I'm just not sure why you'd want to switch these around in this patch?Can we try to keep original ordering unless there's a really good reason not to?
Again, I'm trying to avoid undoing things if locking fails but this function already has rollback code, so I can reuse it. I'll keep the old placement here.
Quoted 78 lines
Quoted 67 lines
mm/vma.c +16
-5if (commit_merge (vmg)) goto nomem;@@ -1233,6 +1246,7 @@ 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));@@ -1244,7 +1258,11 @@ int vma_shrink(struct vma_iterator *vmi, struct vm_area_struct *vma,if (vma_iter_prealloc (vmi,NULL ))return -ENOMEM ; -vma_start_write (vma); + err =vma_start_write_killable (vma); +if (err) { +vma_iter_free (vmi); +return err; + }init_vma_prep (&vp, vma);vma_prepare (&vp);@@ -1434,7 +1452,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)@@ -1828,12 +1848,17 @@ 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;vma_iter_config (&vmi, vma->vm_start, vma->vm_end);if (vma_iter_prealloc (&vmi, vma))return -ENOMEM ; -vma_start_write (vma); + err =vma_start_write_killable (vma); +if (err) { +vma_iter_free (&vmi); +return err; + }vma_iter_store_new (&vmi, vma);vma_link_file (vma,/* hold_rmap_lock= */ false); mm->map_count++;@@ -2215,9 +2240,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 );mm/vma.c static int __mmap_new_vma(struct mmap_state *map, struct vm_area_struct **vmap) · line 2553
Quoted 8 lines
mm/vma.c +5
-0@@ -2522,6 +2546,11 @@ static int __mmap_new_vma(struct mmap_state *map, struct vm_area_struct **vmap)if (!vma)return -ENOMEM ; +/* Lock the VMA since it is modified after insertion into VMA tree */ + error =vma_start_write_killable (vma); +if (error) + goto free_vma; +You're doing it again :)
Quoted 5 lines
Can we please keep the lock acquisition at the point it is in unless there's a really good reason not to.
And if there is a good reason, please do it in another commit prior to the massive 'change everything' one so it's more easily reviewable :)
The reason for this one is that I want to avoid undoing __mmap_new_file_vma() if we fail to lock the VMA later. Undoing that one would be messy, so I would prefer locking it earlier. These operations are already performed under the mmap write lock. Is that really a problem if we write-lock the VMA as well?
Quoted 23 lines
Quoted 14 lines
mm/vma.c +0
-2vma_iter_config (vmi, map->addr, map->end);vma_set_range (vma, map->addr, map->end, map->pgoff);vm_flags_init (vma, map->vm_flags);@@ -2552,8 +2581,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, map->hold_file_rmap_lock);mm/vma.c int do_brk_flags(struct vma_iterator *vmi, struct vm_area_struct *vma, · line 2894
Quoted 4 lines
mm/vma.c +1
-0@@ -2864,6 +2891,7 @@ int do_brk_flags(struct vma_iterator *vmi, struct vm_area_struct *vma, unsigned long addr, unsigned long len, vm_flags_t vm_flags) { struct mm_struct *mm = current->mm; +int err = -ENOMEM ;I hate this 'default error code' pattern, it's a code smell. Please update everything that jumps to the failure case to set err, and leave this uninitialised.
Quoted 1 line
We've had real bugs come out of this before!
Ack.
Quoted 61 lines
Quoted 53 lines
mm/vma.c +11
-6/* * Check against address space limits by the changed size @@ -2908,7 +2936,10 @@ int do_brk_flags(struct vma_iterator *vmi, struct vm_area_struct *vma,vma_set_range (vma, addr, addr + len, addr >>PAGE_SHIFT );vm_flags_init (vma, vm_flags); vma->vm_page_prot =vm_get_page_prot (vm_flags); -vma_start_write (vma); +if (vma_start_write_killable (vma)) { + err = -EINTR ; + goto mas_store_fail; + }if (vma_iter_store_gfp (vmi, vma,GFP_KERNEL )) goto mas_store_fail;@@ -2928,7 +2959,7 @@ int do_brk_flags(struct vma_iterator *vmi, struct vm_area_struct *vma,vm_area_free (vma); unacct_fail:vm_unacct_memory (len >>PAGE_SHIFT ); -return -ENOMEM ; +return err; }/** @@ -3089,7 +3120,7 @@ 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 ; +int error;VMA_ITERATOR (vmi, mm, vma->vm_start);if (!(vma->vm_flags &VM_GROWSUP ))@@ -3126,12 +3157,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 free; }/* Lock the VMA before expanding to prevent concurrent page faults */ -vma_start_write (vma); + error =vma_start_write_killable (vma); +if (error) + goto free;/* We update the anon VMA tree. */ anon_vma_lock_write (vma->anon_vma);mm/vma.c int expand_upwards(struct vm_area_struct *vma, unsigned long address) · line 3196
Quoted 4 lines
mm/vma.c +1
-0@@ -3160,6 +3193,7 @@ int expand_upwards(struct vm_area_struct *vma, unsigned long address) } }anon_vma_unlock_write (vma->anon_vma); +free:Nitty, but this kinda sucks as a label name, generally when the error label contains 'free' it is
free_xxxwhere 'xxx' is some specific thing.
Quoted 1 line
So somethiing like 'fail' would be good.
Ack. Will change to something more appropriate.
Quoted 31 lines
Quoted 31 lines
mm/vma.c +6
-4vma_iter_free (&vmi);validate_mm (mm);return error;@@ -3174,7 +3208,7 @@ 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 ; +int error;VMA_ITERATOR (vmi, mm, vma->vm_start);if (!(vma->vm_flags &VM_GROWSDOWN ))@@ -3205,12 +3239,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 free; }/* Lock the VMA before expanding to prevent concurrent page faults */ -vma_start_write (vma); + error =vma_start_write_killable (vma); +if (error) + goto free;/* We update the anon VMA tree. */ anon_vma_lock_write (vma->anon_vma);
Quoted 7 lines
mm/vma.c int expand_downwards(struct vm_area_struct *vma, unsigned long address) · line 3279
Quoted 5 lines
mm/vma.c +1
-0@@ -3240,6 +3276,7 @@ int expand_downwards(struct vm_area_struct *vma, unsigned long address) } }anon_vma_unlock_write (vma->anon_vma); +free:Obviously same comment her :)
Ack.
Quoted 16 lines
Quoted 16 lines
mm/vma.c +0
-0vma_iter_free (&vmi);validate_mm (mm);return error;mm/vma_exec.c +1
-0@@ -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);
Quoted 13 lines
mm/vma_exec.c int relocate_vma_down(struct vm_area_struct *vma, unsigned long shift) · line 61
Quoted 9 lines
mm/vma_exec.c +3
-2@@ -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;Hmm. But before we were filtering the errors and now we're not... I guess not an issue as before it could _only_ return
-ENOMEM, but again, are we sure all callers are fine with-EINTRI guess :)
This function is called only from setup_arg_pages() and all its callers end up being linux_binfmt.load_binary handlers. The returned error propagates all the way to execve() and its friends. And with my extreme "luck" the execve syscall lists probably every single possible error code except EINTR :) This is depressing...
Thanks for the detailed review, Lorenzo! I guess we need to discuss these lock moves a bit more before I start on the new version.
Quoted 10 lines
Quoted 7 lines
mm/vma_exec.c +0
-1/* * move the page tables downwards, on failure we rely on -- 2.53.0.414.gf7e9f6c205-goog
Cheers, Lorenzo
Re: [PATCH v3 2/3] mm: replace vma_start_write() with vma_start_write_killable()
Quoted 10 lines · Suren Baghdasaryan
Quoted 3 lines · Lorenzo Stoakes
Overall I'm a little concerned about whether callers can handle
-EINTRin all cases, have you checked? Might we cause some weirdness in userspace if a syscall suddenly returns-EINTRwhen before it didn't?I did check the kernel users and put the patchset through AI reviews. I haven't checked if any of the affected syscalls do not advertise
-EINTRas a possible error. Adding that to my todo list for the next respin.
This only allows interruption by *fatal* signals. ie there's no way that userspace will see -EINTR because it's dead before the syscall returns to userspace. That was the whole point of killable instead of interruptible.
Suren Baghdasaryan
reviewerRe: [PATCH v3 2/3] mm: replace vma_start_write() with vma_start_write_killable()
Quoted 13 lines · Matthew Wilcox
Quoted 10 lines · Suren Baghdasaryan
Quoted 3 lines · Lorenzo Stoakes
Overall I'm a little concerned about whether callers can handle
-EINTRin all cases, have you checked? Might we cause some weirdness in userspace if a syscall suddenly returns-EINTRwhen before it didn't?I did check the kernel users and put the patchset through AI reviews. I haven't checked if any of the affected syscalls do not advertise
-EINTRas a possible error. Adding that to my todo list for the next respin.
Quoted 4 lines
This only allows interruption by *fatal* signals. ie there's no way that userspace will see
-EINTRbecause it's dead before the syscall returns to userspace. That was the whole point of killable instead of interruptible.
Ah, I see. So, IIUC, that means any syscall can potentially fail with -EINTR and this failure code doesn't need to be documented. Is that right?
Re: [PATCH v3 2/3] mm: replace vma_start_write() with vma_start_write_killable()
Quoted 14 lines · Suren Baghdasaryan
Quoted 13 lines · Matthew Wilcox
Quoted 10 lines · Suren Baghdasaryan
Quoted 3 lines · Lorenzo Stoakes
Overall I'm a little concerned about whether callers can handle
-EINTRin all cases, have you checked? Might we cause some weirdness in userspace if a syscall suddenly returns-EINTRwhen before it didn't?I did check the kernel users and put the patchset through AI reviews. I haven't checked if any of the affected syscalls do not advertise
-EINTRas a possible error. Adding that to my todo list for the next respin.
Quoted 8 lines
Quoted 4 lines
This only allows interruption by *fatal* signals. ie there's no way that userspace will see
-EINTRbecause it's dead before the syscall returns to userspace. That was the whole point of killable instead of interruptible.Ah, I see. So, IIUC, that means any syscall can potentially fail with
-EINTRand this failure code doesn't need to be documented. Is that right?
We could literally return any error code -- it never makes it to userspace. I forget where it is, but if you follow the syscall return to user path, a dying task never makes it to running a single instruction.
Lorenzo Stoakes (Oracle)
maintainerRe: [PATCH v3 2/3] mm: replace vma_start_write() with vma_start_write_killable()
Quoted 20 lines · Matthew Wilcox
Quoted 19 lines · Suren Baghdasaryan
Quoted 17 lines · Matthew Wilcox
Quoted 10 lines · Suren Baghdasaryan
Quoted 3 lines · Lorenzo Stoakes
Overall I'm a little concerned about whether callers can handle
-EINTRin all cases, have you checked? Might we cause some weirdness in userspace if a syscall suddenly returns-EINTRwhen before it didn't?I did check the kernel users and put the patchset through AI reviews. I haven't checked if any of the affected syscalls do not advertise
-EINTRas a possible error. Adding that to my todo list for the next respin.This only allows interruption by *fatal* signals. ie there's no way that userspace will see
-EINTRbecause it's dead before the syscall returns to userspace. That was the whole point of killable instead of interruptible.
Quoted 8 lines
Quoted 3 lines
Ah, I see. So, IIUC, that means any syscall can potentially fail with
-EINTRand this failure code doesn't need to be documented. Is that right?We could literally return any error code -- it never makes it to userspace. I forget where it is, but if you follow the syscall return to user path, a dying task never makes it to running a single instruction.
Thanks for that Matthew, that makes life easier then.
We can probably replace some of the more horrid if (err == -EINTR) stuff with fatal_signal_pending(current) to be clearer as a result.
Cheers, Lorenzo
Re: [PATCH v3 2/3] mm: replace vma_start_write() with vma_start_write_killable()
Quoted 10 lines · Lorenzo Stoakes (Oracle)
Quoted 4 lines · Matthew Wilcox
We could literally return any error code -- it never makes it to userspace. I forget where it is, but if you follow the syscall return to user path, a dying task never makes it to running a single instruction.
Thanks for that Matthew, that makes life easier then.
We can probably replace some of the more horrid if (err ==
-EINTR) stuff withfatal_signal_pending(current)to be clearer as a result.
Umm. Be careful? fatal_signal_pending() may become true at a later point, so you may have acquired the lock _and_ fatal_signal_pending() can be true. I'd need to audit a patch to be sure that it's a reasonable replacement.
Suren Baghdasaryan
reviewerRe: [PATCH v3 2/3] mm: replace vma_start_write() with vma_start_write_killable()
Quoted 10 lines · Matthew Wilcox
Quoted 8 lines · Lorenzo Stoakes (Oracle)
Quoted 4 lines · Matthew Wilcox
We could literally return any error code -- it never makes it to userspace. I forget where it is, but if you follow the syscall return to user path, a dying task never makes it to running a single instruction.
Thanks for that Matthew, that makes life easier then.
Quoted 7 lines
Quoted 2 lines
We can probably replace some of the more horrid if (err ==
-EINTR) stuff withfatal_signal_pending(current)to be clearer as a result.Umm. Be careful?
fatal_signal_pending()may become true at a later point, so you may have acquired the lock _and_fatal_signal_pending()can be true. I'd need to audit a patch to be sure that it's a reasonable replacement.
Good point. I'll evaluate each case for this potential. Thanks!
Lorenzo Stoakes (Oracle)
maintainerRe: [PATCH v3 2/3] mm: replace vma_start_write() with vma_start_write_killable()
In reply to Suren BaghdasaryanQuoted 38 lines · Suren Baghdasaryan
Quoted 17 lines · Lorenzo Stoakes
Quoted 6 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 patch:
Quoted 8 lines
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.Quoted 6 lines
Surprise surprise :))
Quoted 2 lines
4.
mpol_rebind_mm()which is used by cpusset controller for migrationsIncredibly nitty but cpusset -> cpuset?
Ack.
Quoted 10 lines
Quoted 9 lines
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 withvma_assert_write_locked(), then callers of these functions should lock the vma themselves usingvma_start_write_killable()whenever possible.
Quoted 4 lines
Quoted 1 line
This should be dealt with by my ongoing
mmap_prepare, vma flags work.That would be great! It makes it much simpler once you are done with that refactoring.
Yeah will be a big series ;) stats++;
Quoted 7 lines
Quoted 7 lines
Quoted 6 lines
Suggested-by Matthew Wilcox <[email protected]>Signed-off-by Suren Baghdasaryan <[email protected]>
Quoted 8 lines
Quoted 3 lines
Overall I'm a little concerned about whether callers can handle
-EINTRin all cases, have you checked? Might we cause some weirdness in userspace if a syscall suddenly returns-EINTRwhen before it didn't?I did check the kernel users and put the patchset through AI reviews. I haven't checked if any of the affected syscalls do not advertise
-EINTRas a possible error. Adding that to my todo list for the next respin.
Ah yeah as per Matthew's, fair enough. And yes it's only fatal signals.
But then is there any point in doing special handling/not filtering? We may as well not :)
I also don't want us to run into an issue where something other than a fatal error results in an -EINTR.
Quoted 8 lines
Quoted 4 lines
Also maybe we should update the manpages to reflect this, as semi-usless as the 'possible error codes' sections are...
Ok, I'll check which syscalls need to be updated and will note that in cover letter. Once the patchset is accepted I'll update the manpages for them.
Yeah obv nop as per Matthew.
Quoted 90 lines
Quoted 81 lines
Quoted 81 lines
--- arch/powerpc/kvm/book3s_hv_uvmem.c | 5 +- mm/khugepaged.c | 5 +- mm/madvise.c | 4 +- mm/memory.c | 2 + mm/mempolicy.c | 8 ++- mm/mlock.c | 21 +++++-- mm/mprotect.c | 4 +- mm/mremap.c | 4 +- mm/vma.c | 93 +++++++++++++++++++++--------- mm/vma_exec.c | 6 +- 10 files changed, 109 insertions(+), 43 deletions(-)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,mm/khugepaged.c +4
-1@@ -1141,7 +1141,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;mm/madvise.c +3
-1@@ -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@@ -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. +* 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 +0
-0@@ -1784,7 +1784,8 @@ SYSCALL_DEFINE4(set_mempolicy_home_node, unsigned long, start, unsigned long, lereturn -EINVAL ;Quoted 8 lines
mm/mempolicy.c SYSCALL_DEFINE4(set_mempolicy_home_node, unsigned long, start, unsigned long, le · line 1788
Quoted 5 lines
mm/mempolicy.c +2
-1if (end == start)return 0 ; -mmap_write_lock (mm); +if (mmap_write_lock_killable (mm)) +return -EINTR ;Hmm mmap write lock as well now :) I guess it makes sense here, esp given mmap write lock part of VMA write lock.
Quoted 1 line
Yeah, I thought while we are at it we can make this part a bit better too...
Yup that's fair.
Quoted 25 lines
Quoted 20 lines
Quoted 20 lines
mm/mempolicy.c +4
-0prev =vma_prev (&vmi);for_each_vma_range (vmi, vma, end) {/* @@ -1801,13 +1802,16 @@ SYSCALL_DEFINE4(set_mempolicy_home_node, unsigned long, start, unsigned long, le err = -EOPNOTSUPP ;break ; } +if (vma_start_write_killable (vma)) { + err = -EINTR ; +break ; + } new =mpol_dup (old);if (IS_ERR (new)) { err =PTR_ERR (new);break ; }Quoted 4 lines
mm/mempolicy.c SYSCALL_DEFINE4(set_mempolicy_home_node, unsigned long, start, unsigned long, le · line 1810
Quoted 1 line
mm/mempolicy.c +0
-1-vma_start_write (vma);Are we ok with moving this to before
mpol_dup()? Does this matter? Confused as to why you moved this up?
Quoted 4 lines
I thought if locking fails, it would be better to check that earlier before allocating a new mempolicy. That seems to be safe, but thinking about this now, if allocation goes into direct reclaim and causes the lock to be held for longer then that might not be such a hot idea...
Yeah :) it's stuff like this that makes me worry. But moreso some horrendous lock ordering issue...
Quoted 16 lines
Quoted 15 lines
mm/mlock.c static int mlock_pte_range(pmd_t *pmd, unsigned long addr, · line 422
Quoted 13 lines
mm/mempolicy.c +0
-0new->home_node = home_node; err =mbind_range (&vmi, vma, &prev, start, end, new);mpol_put (new);mm/mlock.c +0
-0@@ -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. */ You should update the comment to reflect this possible return value.
Quoted 1 line
Ack.
Thanks!
Quoted 34 lines
Quoted 25 lines
Quoted 25 lines
mm/mlock.c +5
-2-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 ; }Quoted 8 lines
mm/mlock.c static int mlock_fixup(struct vma_iterator *vmi, struct vm_area_struct *vma, · line 506
Quoted 5 lines
mm/mlock.c +0
-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 */ I'd move this comment down to the
vm_flags_reset()line as it's not applicable if we fail to get the lock.
Quoted 1 line
Ack.
Thanks!
Quoted 24 lines
Quoted 16 lines
Quoted 16 lines
mm/mlock.c +4
-2-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,13 @@ static int apply_mlockall_flags(int flags)Quoted 7 lines
mm/mlock.c static int apply_mlockall_flags(int flags) · line 741
Quoted 5 lines
mm/mlock.c +1
-2error =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. */ Well, except you're not fixing it up on
-EINTR? This comment should be redone.
Quoted 2 lines
Hmm, should we fixup if the process is terminating? Does it matter at this point?
We may as well keep the comment accurate though? I mean this isn't a big deal :)
Quoted 8 lines
Quoted 4 lines
But I wonder if this is correct? We are ignoring all other errors that interrupted the operation, so why are we special casing
-EINTR?Well,
-EINTRmeans all the work you are doing here is useless because the process is about to go away. So, in that respect I think it's different from other errors.
Well -EINTR doesn't _just_ mean fatal signal. We happen to know here that what we're calling will only return -EINTR in that case, but if somebody changes downstream code to return -EINTR for _another_ reason there's an issue.
Can we just replace these horrid -EINTR checks with is_fatal_signal_pending(current)?
Quoted 25 lines
Quoted 16 lines
Quoted 16 lines
mm/mlock.c +5
-0+if (error) { +if (error == -EINTR ) +return error; + prev = vma; + }cond_resched (); } out:mm/mprotect.c +0
-0@@ -765,7 +765,9 @@ mprotect_fixup(struct vma_iterator *vmi, struct mmu_gather *tlb,Quoted 8 lines
mm/mprotect.c mprotect_fixup(struct vma_iterator *vmi, struct mmu_gather *tlb, · line 769
Quoted 6 lines
mm/mprotect.c +2
-1* 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 )Weird inconstency here, this should be if (error).
Quoted 1 line
Ack.
Thanks!
Quoted 52 lines
Quoted 43 lines
Quoted 43 lines
mm/mprotect.c +1
-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@@ -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/vma.c +7
-0@@ -530,6 +530,13 @@ __split_vma(struct vma_iterator *vmi, struct vm_area_struct *vma,if (err) goto out_free_vmi; + err =vma_start_write_killable (vma); +if (err) + goto out_free_mpol; + err =vma_start_write_killable (new); +if (err) + goto out_free_mpol; + err =anon_vma_clone (new, vma,VMA_OP_SPLIT );if (err) goto out_free_mpol;@@ -540,9 +547,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);Quoted 5 lines
mm/vma.c __split_vma(struct vma_iterator *vmi, struct vm_area_struct *vma, · line 545
Quoted 3 lines
mm/vma.c +0
-3-vma_start_write (vma); -vma_start_write (new); -Again you're changing ordering seemingly arbitrarily.
I moved it up to avoid undoing all the work above (
vm_ops->open,vma_dup_policy,vma_iter_prealloc,vm_area_dup,..)
Quoted 8 lines
Quoted 3 lines
I think this is actually a
more problematic case as you're now invokingvm_ops->open()with a VMA write lock held.Are you concerned about potential increased duration of the vma lock being held or that
open()might try to take that lock itself (which is not a problem because vma write locks are reentrant)? Maybe some other concern I'm missing?
General concern about reordering, but fair point re: undoing.
rmap.c says it's ok so we're good:
* vma_start_write
* mapping->i_mmap_rwsem
* anon_vma->rwsem
Quoted 6 lines
Quoted 3 lines
So I think you should keep the existing position.
If we do that then we would have to undo a bunch of operations. I'm fine adding that if there are reasons to avoid this move.
OK fair enough in this case.
Quoted 70 lines
Quoted 70 lines
Quoted 57 lines
mm/vma.c +16
-5init_vma_prep (&vp, vma); vp.insert = new;vma_prepare (&vp);@@ -895,16 +899,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) + goto abort;if (merge_right) { -vma_start_write (next); + err =vma_start_write_killable (next); +if (err) + goto abort; vmg->target = next; sticky_flags |= (next->vm_flags &VM_STICKY ); }if (merge_left) { -vma_start_write (prev); + err =vma_start_write_killable (prev); +if (err) + goto abort; vmg->target = prev; sticky_flags |= (prev->vm_flags &VM_STICKY ); }@@ -1155,10 +1165,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;@@ -1187,6 +1199,9 @@ int vma_expand(struct vma_merge_struct *vmg)* we don't need to account for vmg->give_up_on_mm here. */ if (remove_next) { + ret =vma_start_write_killable (next); +if (ret) +return ret; ret =dup_anon_vma (target, next, &anon_dup);if (ret)return ret;mm/vma.c int vma_expand(struct vma_merge_struct *vmg) · line 1203
Quoted 8 lines
mm/vma.c +1
-3@@ -1197,10 +1212,8 @@ int vma_expand(struct vma_merge_struct *vmg)return ret; } -if (remove_next) { -vma_start_write (next); +if (remove_next) vmg->__remove_next = true; - }Hmm you're moving the ordering of things around again :) You should have made this change as part of patch 1 anyway first so this patch wouldn't have a refactoring in it too.
Quoted 8 lines
Quoted 4 lines
Top of
rmap.csuggests you _can_ take the VMA write lock prior to trying the dup but I'm just not sure why you'd want to switch these around in this patch?Can we try to keep original ordering unless there's a really good reason not to?
Again, I'm trying to avoid undoing things if locking fails but this function already has rollback code, so I can reuse it. I'll keep the old placement here.
Ack yeah... :)
We're safe with vma -> anon rmap lock anyway.
Quoted 82 lines
Quoted 82 lines
Quoted 68 lines
mm/vma.c +16
-5if (commit_merge (vmg)) goto nomem;@@ -1233,6 +1246,7 @@ 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));@@ -1244,7 +1258,11 @@ int vma_shrink(struct vma_iterator *vmi, struct vm_area_struct *vma,if (vma_iter_prealloc (vmi,NULL ))return -ENOMEM ; -vma_start_write (vma); + err =vma_start_write_killable (vma); +if (err) { +vma_iter_free (vmi); +return err; + }init_vma_prep (&vp, vma);vma_prepare (&vp);@@ -1434,7 +1452,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)@@ -1828,12 +1848,17 @@ 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;vma_iter_config (&vmi, vma->vm_start, vma->vm_end);if (vma_iter_prealloc (&vmi, vma))return -ENOMEM ; -vma_start_write (vma); + err =vma_start_write_killable (vma); +if (err) { +vma_iter_free (&vmi); +return err; + }vma_iter_store_new (&vmi, vma);vma_link_file (vma,/* hold_rmap_lock= */ false); mm->map_count++;@@ -2215,9 +2240,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 );mm/vma.c static int __mmap_new_vma(struct mmap_state *map, struct vm_area_struct **vmap) · line 2553
Quoted 8 lines
mm/vma.c +5
-0@@ -2522,6 +2546,11 @@ static int __mmap_new_vma(struct mmap_state *map, struct vm_area_struct **vmap)if (!vma)return -ENOMEM ; +/* Lock the VMA since it is modified after insertion into VMA tree */ + error =vma_start_write_killable (vma); +if (error) + goto free_vma; +You're doing it again :)
Can we please keep the lock acquisition at the point it is in unless there's a really good reason not to.
Quoted 8 lines
Quoted 2 lines
And if there is a good reason, please do it in another commit prior to the massive 'change everything' one so it's more easily reviewable :)
The reason for this one is that I want to avoid undoing
__mmap_new_file_vma()if we fail to lock the VMA later. Undoing that one would be messy, so I would prefer locking it earlier. These operations are already performed under the mmap write lock. Is that really a problem if we write-lock the VMA as well?
This is a fair point, I obviously glanced over this without thinking about that :)
I think you should include in the commit message why you're reordering though.
Quoted 24 lines
Quoted 24 lines
Quoted 15 lines
mm/vma.c +0
-2vma_iter_config (vmi, map->addr, map->end);vma_set_range (vma, map->addr, map->end, map->pgoff);vm_flags_init (vma, map->vm_flags);@@ -2552,8 +2581,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, map->hold_file_rmap_lock);mm/vma.c int do_brk_flags(struct vma_iterator *vmi, struct vm_area_struct *vma, · line 2894
Quoted 4 lines
mm/vma.c +1
-0@@ -2864,6 +2891,7 @@ int do_brk_flags(struct vma_iterator *vmi, struct vm_area_struct *vma, unsigned long addr, unsigned long len, vm_flags_t vm_flags) { struct mm_struct *mm = current->mm; +int err = -ENOMEM ;I hate this 'default error code' pattern, it's a code smell. Please update everything that jumps to the failure case to set err, and leave this uninitialised.
Quoted 3 lines
Quoted 1 line
We've had real bugs come out of this before!
Ack.
Thanks!
Quoted 62 lines
Quoted 62 lines
Quoted 54 lines
mm/vma.c +11
-6/* * Check against address space limits by the changed size @@ -2908,7 +2936,10 @@ int do_brk_flags(struct vma_iterator *vmi, struct vm_area_struct *vma,vma_set_range (vma, addr, addr + len, addr >>PAGE_SHIFT );vm_flags_init (vma, vm_flags); vma->vm_page_prot =vm_get_page_prot (vm_flags); -vma_start_write (vma); +if (vma_start_write_killable (vma)) { + err = -EINTR ; + goto mas_store_fail; + }if (vma_iter_store_gfp (vmi, vma,GFP_KERNEL )) goto mas_store_fail;@@ -2928,7 +2959,7 @@ int do_brk_flags(struct vma_iterator *vmi, struct vm_area_struct *vma,vm_area_free (vma); unacct_fail:vm_unacct_memory (len >>PAGE_SHIFT ); -return -ENOMEM ; +return err; }/** @@ -3089,7 +3120,7 @@ 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 ; +int error;VMA_ITERATOR (vmi, mm, vma->vm_start);if (!(vma->vm_flags &VM_GROWSUP ))@@ -3126,12 +3157,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 free; }/* Lock the VMA before expanding to prevent concurrent page faults */ -vma_start_write (vma); + error =vma_start_write_killable (vma); +if (error) + goto free;/* We update the anon VMA tree. */ anon_vma_lock_write (vma->anon_vma);mm/vma.c int expand_upwards(struct vm_area_struct *vma, unsigned long address) · line 3196
Quoted 4 lines
mm/vma.c +1
-0@@ -3160,6 +3193,7 @@ int expand_upwards(struct vm_area_struct *vma, unsigned long address) } }anon_vma_unlock_write (vma->anon_vma); +free:Nitty, but this kinda sucks as a label name, generally when the error label contains 'free' it is
free_xxxwhere 'xxx' is some specific thing.
Quoted 3 lines
Quoted 1 line
So somethiing like 'fail' would be good.
Ack. Will change to something more appropriate.
Thanks!
Quoted 40 lines
Quoted 32 lines
Quoted 32 lines
mm/vma.c +6
-4vma_iter_free (&vmi);validate_mm (mm);return error;@@ -3174,7 +3208,7 @@ 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 ; +int error;VMA_ITERATOR (vmi, mm, vma->vm_start);if (!(vma->vm_flags &VM_GROWSDOWN ))@@ -3205,12 +3239,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 free; }/* Lock the VMA before expanding to prevent concurrent page faults */ -vma_start_write (vma); + error =vma_start_write_killable (vma); +if (error) + goto free;/* We update the anon VMA tree. */ anon_vma_lock_write (vma->anon_vma);Quoted 7 lines
mm/vma.c int expand_downwards(struct vm_area_struct *vma, unsigned long address) · line 3279
Quoted 5 lines
mm/vma.c +1
-0@@ -3240,6 +3276,7 @@ int expand_downwards(struct vm_area_struct *vma, unsigned long address) } }anon_vma_unlock_write (vma->anon_vma); +free:Obviously same comment her :)
Quoted 1 line
Ack.
Thanks!
Quoted 31 lines
Quoted 17 lines
Quoted 17 lines
mm/vma.c +0
-0vma_iter_free (&vmi);validate_mm (mm);return error;mm/vma_exec.c +1
-0@@ -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);Quoted 13 lines
mm/vma_exec.c int relocate_vma_down(struct vm_area_struct *vma, unsigned long shift) · line 61
Quoted 9 lines
mm/vma_exec.c +3
-2@@ -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;Hmm. But before we were filtering the errors and now we're not... I guess not an issue as before it could _only_ return
-ENOMEM, but again, are we sure all callers are fine with-EINTRI guess :)
Quoted 5 lines
This function is called only from
setup_arg_pages()and all its callers end up being linux_binfmt.load_binary handlers. The returned error propagates all the way toexecve()and its friends. And with my extreme "luck" the execve syscall lists probably every single possible error code except EINTR :) This is depressing...
Well as per Matthew's point this is probably fine.
Quoted 3 lines
Thanks for the detailed review, Lorenzo! I guess we need to discuss these lock moves a bit more before I start on the new version.
Nah I think we're good after this!
Quoted 11 lines
Quoted 11 lines
Quoted 8 lines
mm/vma_exec.c +0
-1/* * move the page tables downwards, on failure we rely on -- 2.53.0.414.gf7e9f6c205-goog
Cheers, Lorenzo
Cheers, Lorenzo
Suren Baghdasaryan
reviewerRe: [PATCH v3 2/3] mm: replace vma_start_write() with vma_start_write_killable()
Quoted 63 lines · Lorenzo Stoakes (Oracle)
Quoted 38 lines · Suren Baghdasaryan
Quoted 17 lines · Lorenzo Stoakes
Quoted 6 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 patch:
Quoted 8 lines
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.Quoted 6 lines
Surprise surprise :))
Quoted 2 lines
4.
mpol_rebind_mm()which is used by cpusset controller for migrationsIncredibly nitty but cpusset -> cpuset?
Ack.
Quoted 10 lines
Quoted 9 lines
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 withvma_assert_write_locked(), then callers of these functions should lock the vma themselves usingvma_start_write_killable()whenever possible.Quoted 4 lines
Quoted 1 line
This should be dealt with by my ongoing
mmap_prepare, vma flags work.That would be great! It makes it much simpler once you are done with that refactoring.
Yeah will be a big series ;) stats++;
Quoted 7 lines
Quoted 7 lines
Quoted 6 lines
Suggested-by Matthew Wilcox <[email protected]>Signed-off-by Suren Baghdasaryan <[email protected]>Quoted 8 lines
Quoted 3 lines
Overall I'm a little concerned about whether callers can handle
-EINTRin all cases, have you checked? Might we cause some weirdness in userspace if a syscall suddenly returns-EINTRwhen before it didn't?I did check the kernel users and put the patchset through AI reviews. I haven't checked if any of the affected syscalls do not advertise
-EINTRas a possible error. Adding that to my todo list for the next respin.
Quoted 7 lines
Ah yeah as per Matthew's, fair enough. And yes it's only fatal signals.
But then is there any point in doing special handling/not filtering? We may as well not :)
I also don't want us to run into an issue where something other than a fatal error results in an
-EINTR.
Ok, that's a good point. I'll reconsider the special handling of -EINTR.
Quoted 11 lines
Quoted 9 lines
Quoted 5 lines
Also maybe we should update the manpages to reflect this, as semi-usless as the 'possible error codes' sections are...
Ok, I'll check which syscalls need to be updated and will note that in cover letter. Once the patchset is accepted I'll update the manpages for them.
Yeah obv nop as per Matthew.
Ack.
Quoted 120 lines
Quoted 91 lines
Quoted 82 lines
Quoted 82 lines
--- arch/powerpc/kvm/book3s_hv_uvmem.c | 5 +- mm/khugepaged.c | 5 +- mm/madvise.c | 4 +- mm/memory.c | 2 + mm/mempolicy.c | 8 ++- mm/mlock.c | 21 +++++-- mm/mprotect.c | 4 +- mm/mremap.c | 4 +- mm/vma.c | 93 +++++++++++++++++++++--------- mm/vma_exec.c | 6 +- 10 files changed, 109 insertions(+), 43 deletions(-)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,mm/khugepaged.c +4
-1@@ -1141,7 +1141,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;mm/madvise.c +3
-1@@ -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@@ -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. +* 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 +0
-0@@ -1784,7 +1784,8 @@ SYSCALL_DEFINE4(set_mempolicy_home_node, unsigned long, start, unsigned long, lereturn -EINVAL ;Quoted 8 lines
mm/mempolicy.c SYSCALL_DEFINE4(set_mempolicy_home_node, unsigned long, start, unsigned long, le · line 1788
Quoted 5 lines
mm/mempolicy.c +2
-1if (end == start)return 0 ; -mmap_write_lock (mm); +if (mmap_write_lock_killable (mm)) +return -EINTR ;Hmm mmap write lock as well now :) I guess it makes sense here, esp given mmap write lock part of VMA write lock.
Quoted 1 line
Yeah, I thought while we are at it we can make this part a bit better too...
Yup that's fair.
Quoted 25 lines
Quoted 24 lines
Quoted 11 lines
mm/mempolicy.c +1
-0prev =vma_prev (&vmi);for_each_vma_range (vmi, vma, end) {/* @@ -1801,13 +1802,16 @@ SYSCALL_DEFINE4(set_mempolicy_home_node, unsigned long, start, unsigned long, le err = -EOPNOTSUPP ;break ; } +if (vma_start_write_killable (vma)) {mm/mempolicy.c SYSCALL_DEFINE4(set_mempolicy_home_node, unsigned long, start, unsigned long, le · line 1810
Quoted 10 lines
mm/mempolicy.c +3
-1+ err = -EINTR ; +break ; + } new =mpol_dup (old);if (IS_ERR (new)) { err =PTR_ERR (new);break ; } -vma_start_write (vma);Are we ok with moving this to before
mpol_dup()? Does this matter? Confused as to why you moved this up?
Quoted 7 lines
Quoted 4 lines
I thought if locking fails, it would be better to check that earlier before allocating a new mempolicy. That seems to be safe, but thinking about this now, if allocation goes into direct reclaim and causes the lock to be held for longer then that might not be such a hot idea...
Yeah :) it's stuff like this that makes me worry. But moreso some horrendous lock ordering issue...
I think we are good on lock ordering since we already hold the mmap write lock but I'll try to minimize the lock movements.
Quoted 83 lines
Quoted 17 lines
Quoted 16 lines
Quoted 11 lines
mm/mempolicy.c +0
-0new->home_node = home_node; err =mbind_range (&vmi, vma, &prev, start, end, new);mpol_put (new);mm/mlock.c +0
-0mm/mlock.c static int mlock_pte_range(pmd_t *pmd, unsigned long addr, · line 422
Quoted 3 lines
mm/mlock.c +0
-0@@ -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. */ You should update the comment to reflect this possible return value.
Quoted 1 line
Ack.
Thanks!
Quoted 34 lines
Quoted 25 lines
Quoted 25 lines
mm/mlock.c +5
-2-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 ; }Quoted 8 lines
mm/mlock.c static int mlock_fixup(struct vma_iterator *vmi, struct vm_area_struct *vma, · line 506
Quoted 5 lines
mm/mlock.c +0
-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 */ I'd move this comment down to the
vm_flags_reset()line as it's not applicable if we fail to get the lock.Quoted 1 line
Ack.
Thanks!
Quoted 24 lines
Quoted 23 lines
Quoted 15 lines
mm/mlock.c +4
-2-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;mm/mlock.c static int apply_mlockall_flags(int flags) · line 741
Quoted 6 lines
mm/mlock.c +1
-2@@ -733,9 +738,13 @@ 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. */ Well, except you're not fixing it up on
-EINTR? This comment should be redone.
Quoted 4 lines
Quoted 2 lines
Hmm, should we fixup if the process is terminating? Does it matter at this point?
We may as well keep the comment accurate though? I mean this isn't a big deal :)
Ack.
Quoted 6 lines
Quoted 6 lines
Quoted 5 lines
But I wonder if this is correct? We are ignoring all other errors that interrupted the operation, so why are we special casing
-EINTR?
Quoted 7 lines
Quoted 3 lines
Well,
-EINTRmeans all the work you are doing here is useless because the process is about to go away. So, in that respect I think it's different from other errors.Well
-EINTRdoesn't _just_ mean fatal signal. We happen to know here that what we're calling will only return-EINTRin that case, but if somebody changes downstream code to return-EINTRfor _another_ reason there's an issue.
Fair point.
Quoted 3 lines
Can we just replace these horrid
-EINTRchecks withis_fatal_signal_pending(current)?
I think here that would be fine. I'll evaluate each individual case I check for EINTR.
Quoted 86 lines
Quoted 26 lines
Quoted 17 lines
Quoted 17 lines
mm/mlock.c +5
-0+if (error) { +if (error == -EINTR ) +return error; + prev = vma; + }cond_resched (); } out:mm/mprotect.c +0
-0@@ -765,7 +765,9 @@ mprotect_fixup(struct vma_iterator *vmi, struct mmu_gather *tlb,Quoted 8 lines
mm/mprotect.c mprotect_fixup(struct vma_iterator *vmi, struct mmu_gather *tlb, · line 769
Quoted 6 lines
mm/mprotect.c +2
-1* 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 )Weird inconstency here, this should be if (error).
Quoted 1 line
Ack.
Thanks!
Quoted 56 lines
Quoted 43 lines
Quoted 43 lines
mm/mprotect.c +1
-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@@ -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/vma.c +7
-0@@ -530,6 +530,13 @@ __split_vma(struct vma_iterator *vmi, struct vm_area_struct *vma,if (err) goto out_free_vmi; + err =vma_start_write_killable (vma); +if (err) + goto out_free_mpol; + err =vma_start_write_killable (new); +if (err) + goto out_free_mpol; + err =anon_vma_clone (new, vma,VMA_OP_SPLIT );if (err) goto out_free_mpol;@@ -540,9 +547,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);Quoted 5 lines
mm/vma.c __split_vma(struct vma_iterator *vmi, struct vm_area_struct *vma, · line 545
Quoted 3 lines
mm/vma.c +0
-3-vma_start_write (vma); -vma_start_write (new); -Again you're changing ordering seemingly arbitrarily.
I moved it up to avoid undoing all the work above (
vm_ops->open,vma_dup_policy,vma_iter_prealloc,vm_area_dup,..)Quoted 3 lines
I think this is actually a
more problematic case as you're now invokingvm_ops->open()with a VMA write lock held.
Quoted 8 lines
Quoted 4 lines
Are you concerned about potential increased duration of the vma lock being held or that
open()might try to take that lock itself (which is not a problem because vma write locks are reentrant)? Maybe some other concern I'm missing?General concern about reordering, but fair point re: undoing.
rmap.csays it's ok so we're good:
Ack.
Quoted 7 lines
*
vma_start_write
*mapping->i_mmap_rwsem
*anon_vma->rwsemQuoted 2 lines
Quoted 2 lines
Quoted 6 lines
Quoted 4 lines
Quoted 1 line
So I think you should keep the existing position.
If we do that then we would have to undo a bunch of operations. I'm fine adding that if there are reasons to avoid this move.
OK fair enough in this case.
Ack.
Quoted 76 lines
Quoted 76 lines
Quoted 75 lines
Quoted 58 lines
mm/vma.c +16
-5init_vma_prep (&vp, vma); vp.insert = new;vma_prepare (&vp);@@ -895,16 +899,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) + goto abort;if (merge_right) { -vma_start_write (next); + err =vma_start_write_killable (next); +if (err) + goto abort; vmg->target = next; sticky_flags |= (next->vm_flags &VM_STICKY ); }if (merge_left) { -vma_start_write (prev); + err =vma_start_write_killable (prev); +if (err) + goto abort; vmg->target = prev; sticky_flags |= (prev->vm_flags &VM_STICKY ); }@@ -1155,10 +1165,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;@@ -1187,6 +1199,9 @@ int vma_expand(struct vma_merge_struct *vmg)* we don't need to account for vmg->give_up_on_mm here. */ if (remove_next) { + ret =vma_start_write_killable (next); +if (ret) +return ret; ret =dup_anon_vma (target, next, &anon_dup);if (ret)return ret;mm/vma.c int vma_expand(struct vma_merge_struct *vmg) · line 1203
Quoted 8 lines
mm/vma.c +1
-3@@ -1197,10 +1212,8 @@ int vma_expand(struct vma_merge_struct *vmg)return ret; } -if (remove_next) { -vma_start_write (next); +if (remove_next) vmg->__remove_next = true; - }Hmm you're moving the ordering of things around again :) You should have made this change as part of patch 1 anyway first so this patch wouldn't have a refactoring in it too.
Top of
rmap.csuggests you _can_ take the VMA write lock prior to trying the dup but I'm just not sure why you'd want to switch these around in this patch?Can we try to keep original ordering unless there's a really good reason not to?
Quoted 7 lines
Quoted 3 lines
Again, I'm trying to avoid undoing things if locking fails but this function already has rollback code, so I can reuse it. I'll keep the old placement here.
Ack yeah... :)
We're safe with vma -> anon rmap lock anyway.
Ack.
Quoted 92 lines
Quoted 83 lines
Quoted 83 lines
Quoted 69 lines
mm/vma.c +16
-5if (commit_merge (vmg)) goto nomem;@@ -1233,6 +1246,7 @@ 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));@@ -1244,7 +1258,11 @@ int vma_shrink(struct vma_iterator *vmi, struct vm_area_struct *vma,if (vma_iter_prealloc (vmi,NULL ))return -ENOMEM ; -vma_start_write (vma); + err =vma_start_write_killable (vma); +if (err) { +vma_iter_free (vmi); +return err; + }init_vma_prep (&vp, vma);vma_prepare (&vp);@@ -1434,7 +1452,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)@@ -1828,12 +1848,17 @@ 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;vma_iter_config (&vmi, vma->vm_start, vma->vm_end);if (vma_iter_prealloc (&vmi, vma))return -ENOMEM ; -vma_start_write (vma); + err =vma_start_write_killable (vma); +if (err) { +vma_iter_free (&vmi); +return err; + }vma_iter_store_new (&vmi, vma);vma_link_file (vma,/* hold_rmap_lock= */ false); mm->map_count++;@@ -2215,9 +2240,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 );mm/vma.c static int __mmap_new_vma(struct mmap_state *map, struct vm_area_struct **vmap) · line 2553
Quoted 8 lines
mm/vma.c +5
-0@@ -2522,6 +2546,11 @@ static int __mmap_new_vma(struct mmap_state *map, struct vm_area_struct **vmap)if (!vma)return -ENOMEM ; +/* Lock the VMA since it is modified after insertion into VMA tree */ + error =vma_start_write_killable (vma); +if (error) + goto free_vma; +You're doing it again :)
Can we please keep the lock acquisition at the point it is in unless there's a really good reason not to.
Quoted 8 lines
Quoted 2 lines
And if there is a good reason, please do it in another commit prior to the massive 'change everything' one so it's more easily reviewable :)
The reason for this one is that I want to avoid undoing
__mmap_new_file_vma()if we fail to lock the VMA later. Undoing that one would be messy, so I would prefer locking it earlier. These operations are already performed under the mmap write lock. Is that really a problem if we write-lock the VMA as well?
Quoted 4 lines
This is a fair point, I obviously glanced over this without thinking about that :)
I think you should include in the commit message why you're reordering though.
Yeah, I should have explained more about the reasons. I will add a section in the comments about this.
Quoted 174 lines
Quoted 25 lines
Quoted 25 lines
Quoted 16 lines
mm/vma.c +0
-2vma_iter_config (vmi, map->addr, map->end);vma_set_range (vma, map->addr, map->end, map->pgoff);vm_flags_init (vma, map->vm_flags);@@ -2552,8 +2581,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, map->hold_file_rmap_lock);mm/vma.c int do_brk_flags(struct vma_iterator *vmi, struct vm_area_struct *vma, · line 2894
Quoted 4 lines
mm/vma.c +1
-0@@ -2864,6 +2891,7 @@ int do_brk_flags(struct vma_iterator *vmi, struct vm_area_struct *vma, unsigned long addr, unsigned long len, vm_flags_t vm_flags) { struct mm_struct *mm = current->mm; +int err = -ENOMEM ;I hate this 'default error code' pattern, it's a code smell. Please update everything that jumps to the failure case to set err, and leave this uninitialised.
Quoted 3 lines
Quoted 1 line
We've had real bugs come out of this before!
Ack.
Thanks!
Quoted 62 lines
Quoted 62 lines
Quoted 54 lines
mm/vma.c +11
-6/* * Check against address space limits by the changed size @@ -2908,7 +2936,10 @@ int do_brk_flags(struct vma_iterator *vmi, struct vm_area_struct *vma,vma_set_range (vma, addr, addr + len, addr >>PAGE_SHIFT );vm_flags_init (vma, vm_flags); vma->vm_page_prot =vm_get_page_prot (vm_flags); -vma_start_write (vma); +if (vma_start_write_killable (vma)) { + err = -EINTR ; + goto mas_store_fail; + }if (vma_iter_store_gfp (vmi, vma,GFP_KERNEL )) goto mas_store_fail;@@ -2928,7 +2959,7 @@ int do_brk_flags(struct vma_iterator *vmi, struct vm_area_struct *vma,vm_area_free (vma); unacct_fail:vm_unacct_memory (len >>PAGE_SHIFT ); -return -ENOMEM ; +return err; }/** @@ -3089,7 +3120,7 @@ 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 ; +int error;VMA_ITERATOR (vmi, mm, vma->vm_start);if (!(vma->vm_flags &VM_GROWSUP ))@@ -3126,12 +3157,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 free; }/* Lock the VMA before expanding to prevent concurrent page faults */ -vma_start_write (vma); + error =vma_start_write_killable (vma); +if (error) + goto free;/* We update the anon VMA tree. */ anon_vma_lock_write (vma->anon_vma);mm/vma.c int expand_upwards(struct vm_area_struct *vma, unsigned long address) · line 3196
Quoted 4 lines
mm/vma.c +1
-0@@ -3160,6 +3193,7 @@ int expand_upwards(struct vm_area_struct *vma, unsigned long address) } }anon_vma_unlock_write (vma->anon_vma); +free:Nitty, but this kinda sucks as a label name, generally when the error label contains 'free' it is
free_xxxwhere 'xxx' is some specific thing.Quoted 3 lines
Quoted 1 line
So somethiing like 'fail' would be good.
Ack. Will change to something more appropriate.
Thanks!
Quoted 40 lines
Quoted 32 lines
Quoted 32 lines
mm/vma.c +6
-4vma_iter_free (&vmi);validate_mm (mm);return error;@@ -3174,7 +3208,7 @@ 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 ; +int error;VMA_ITERATOR (vmi, mm, vma->vm_start);if (!(vma->vm_flags &VM_GROWSDOWN ))@@ -3205,12 +3239,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 free; }/* Lock the VMA before expanding to prevent concurrent page faults */ -vma_start_write (vma); + error =vma_start_write_killable (vma); +if (error) + goto free;/* We update the anon VMA tree. */ anon_vma_lock_write (vma->anon_vma);Quoted 7 lines
mm/vma.c int expand_downwards(struct vm_area_struct *vma, unsigned long address) · line 3279
Quoted 5 lines
mm/vma.c +1
-0@@ -3240,6 +3276,7 @@ int expand_downwards(struct vm_area_struct *vma, unsigned long address) } }anon_vma_unlock_write (vma->anon_vma); +free:Obviously same comment her :)
Quoted 1 line
Ack.
Thanks!
Quoted 31 lines
Quoted 30 lines
Quoted 18 lines
mm/vma.c +0
-0vma_iter_free (&vmi);validate_mm (mm);return error;mm/vma_exec.c +1
-0@@ -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);mm/vma_exec.c int relocate_vma_down(struct vm_area_struct *vma, unsigned long shift) · line 61
Quoted 8 lines
mm/vma_exec.c +3
-2@@ -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;Hmm. But before we were filtering the errors and now we're not... I guess not an issue as before it could _only_ return
-ENOMEM, but again, are we sure all callers are fine with-EINTRI guess :)
Quoted 7 lines
Quoted 5 lines
This function is called only from
setup_arg_pages()and all its callers end up being linux_binfmt.load_binary handlers. The returned error propagates all the way toexecve()and its friends. And with my extreme "luck" the execve syscall lists probably every single possible error code except EINTR :) This is depressing...Well as per Matthew's point this is probably fine.
Yes, Matthew made my job much easier :)
Quoted 6 lines
Quoted 4 lines
Thanks for the detailed review, Lorenzo! I guess we need to discuss these lock moves a bit more before I start on the new version.
Nah I think we're good after this!
Ack. I'll make the changes we agreed on and will post v4 likely on Monday. Thanks!
Quoted 14 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 8 lines
Quoted 8 lines
Quoted 5 lines
/*
* move the page tables downwards, on failure we rely on
--
2.53.0.414.gf7e9f6c205-googCheers, Lorenzo
Cheers, Lorenzo
Suren Baghdasaryan
reviewerPATCH v3 3/3 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.
--- arch/s390/kvm/kvm-s390.c | 2 +- fs/proc/task_mmu.c | 5 ++++- mm/mempolicy.c | 14 +++++++++++--- mm/pagewalk.c | 20 ++++++++++++++------ mm/vma.c | 22 ++++++++++++++-------- mm/vma.h | 6 ++++++ 6 files changed, 50 insertions(+), 19 deletions(-)
arch/s390/kvm/kvm-s390.c +1 -1
@@ -2948,7 +2948,7 @@ 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 (copy_to_user (argp, &args,sizeof (args))) { +if (r != -EINTR &©_to_user (argp, &args,sizeof (args))) { r = -EFAULT ;break ; }
fs/proc/task_mmu.c +4 -1
@@ -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 <0 ) + count = err;if (type ==CLEAR_REFS_SOFT_DIRTY ) {mmu_notifier_invalidate_range_end (&range);flush_tlb_mm (mm);
mm/mempolicy.c +11 -3
@@ -988,6 +988,8 @@ queue_pages_range(struct mm_struct *mm, unsigned long start, unsigned long end, &queue_pages_lock_vma_walk_ops : &queue_pages_walk_ops; err =walk_page_range (mm, start, end, ops, &qp); +if (err == -EINTR ) +return err;if (!qp.first)/* whole range in hole */
@@ -1309,9 +1311,14 @@ static long migrate_to_node(struct mm_struct *mm, int source, int dest, flags | MPOL_MF_DISCONTIG_OK, &pagelist);mmap_read_unlock (mm); +if (nr_failed == -EINTR ) + err = nr_failed; +if (!list_empty (&pagelist)) { - err =migrate_pages (&pagelist, alloc_migration_target,NULL , - (unsigned long )&mtc,MIGRATE_SYNC ,MR_SYSCALL ,NULL ); +if (!err) + err =migrate_pages (&pagelist, alloc_migration_target, +NULL , (unsigned long )&mtc, +MIGRATE_SYNC ,MR_SYSCALL ,NULL );if (err)putback_movable_pages (&pagelist); }
@@ -1611,7 +1618,8 @@ static long do_mbind(unsigned long start, unsigned long len, MR_MEMPOLICY_MBIND, NULL); } -if (nr_failed && (flags &MPOL_MF_STRICT )) +/* Do not mask EINTR */ +if ((err != -EINTR ) && (nr_failed && (flags &MPOL_MF_STRICT ))) err = -EIO ;if (!list_empty (&pagelist))putback_movable_pages (&pagelist);
mm/pagewalk.c +14 -6
@@ -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 +14 -8
@@ -998,14 +998,18 @@ static __must_check struct vm_area_struct *vma_merge_existing_range(if (anon_dup)unlink_anon_vmas (anon_dup); -/* -* This means we have failed to clone anon_vma's correctly, but no -* actual changes to VMAs have occurred, so no harm no foul - if the -* user doesn't want this reported and instead just wants to give up on -* the merge, allow it. -*/ -if (!vmg->give_up_on_oom) - vmg->state =VMA_MERGE_ERROR_NOMEM ; +if (err == -EINTR ) { + vmg->state =VMA_MERGE_ERROR_INTR ; + }else { +/* +* This means we have failed to clone anon_vma's correctly, +* but no actual changes to VMAs have occurred, so no harm no +* foul - if the user doesn't want this reported and instead +* just wants to give up on the merge, allow it. +*/ +if (!vmg->give_up_on_oom) + vmg->state =VMA_MERGE_ERROR_NOMEM ; + }return NULL ; }
@@ -1681,6 +1685,8 @@ static struct vm_area_struct *vma_modify(struct vma_merge_struct *vmg) merged =vma_merge_existing_range (vmg);if (merged)return merged; +if (vmg_intr (vmg)) +return ERR_PTR (-EINTR );if (vmg_nomem (vmg))return ERR_PTR (-ENOMEM );
mm/vma.h +6 -0
@@ -56,6 +56,7 @@ struct vma_munmap_struct {enum vma_merge_state {VMA_MERGE_START ,VMA_MERGE_ERROR_NOMEM , +VMA_MERGE_ERROR_INTR ,VMA_MERGE_NOMERGE ,VMA_MERGE_SUCCESS , };
@@ -226,6 +227,11 @@ static inline bool vmg_nomem(struct vma_merge_struct *vmg)return vmg->state ==VMA_MERGE_ERROR_NOMEM ; } +static inline bool vmg_intr (struct vma_merge_struct *vmg) +{ +return vmg->state ==VMA_MERGE_ERROR_INTR ; +} +/* Assumes addr >= vma->vm_start. */ static inline pgoff_t vma_pgoff_offset (struct vm_area_struct *vma,unsigned long addr)
Signature · 1 line
2.53.0.414.gf7e9f6c205-goog
Claudio Imbrenda
maintainerRe: [PATCH v3 3/3] mm: use vma_start_write_killable() in process_vma_walk_lock()
Quoted 21 lines · On Wed, 25 Feb 2026 23:06:09 -0800 Suren Baghdasaryan <[email protected]> wrote:
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.Signed-off-by Suren Baghdasaryan <[email protected]>--- arch/s390/kvm/kvm-s390.c | 2 +- fs/proc/task_mmu.c | 5 ++++- mm/mempolicy.c | 14 +++++++++++--- mm/pagewalk.c | 20 ++++++++++++++------ mm/vma.c | 22 ++++++++++++++-------- mm/vma.h | 6 ++++++ 6 files changed, 50 insertions(+), 19 deletions(-)arch/s390/kvm/kvm-s390.c +0
-0
arch/s390/kvm/kvm-s390.c int kvm_arch_vm_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg) · line 2954
Quoted 8 lines
arch/s390/kvm/kvm-s390.c +1
-1@@ -2948,7 +2948,7 @@ 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 (copy_to_user (argp, &args,sizeof (args))) { +if (r != -EINTR &©_to_user (argp, &args,sizeof (args))) { r = -EFAULT ;break ; }
can you very briefly explain how we can end up with -EINTR here?
do I understand correctly that -EINTR is possible here only if the process is being killed?
[...]
Suren Baghdasaryan
reviewerRe: [PATCH v3 3/3] mm: use vma_start_write_killable() in process_vma_walk_lock()
Quoted 36 lines · Claudio Imbrenda
Quoted 21 lines · On Wed, 25 Feb 2026 23:06:09 -0800 Suren Baghdasaryan <[email protected]> wrote:
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.Signed-off-by Suren Baghdasaryan <[email protected]>--- arch/s390/kvm/kvm-s390.c | 2 +- fs/proc/task_mmu.c | 5 ++++- mm/mempolicy.c | 14 +++++++++++--- mm/pagewalk.c | 20 ++++++++++++++------ mm/vma.c | 22 ++++++++++++++-------- mm/vma.h | 6 ++++++ 6 files changed, 50 insertions(+), 19 deletions(-)arch/s390/kvm/kvm-s390.c +0
-0arch/s390/kvm/kvm-s390.c int kvm_arch_vm_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg) · line 2954
Quoted 8 lines
arch/s390/kvm/kvm-s390.c +1
-1@@ -2948,7 +2948,7 @@ 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 (copy_to_user (argp, &args,sizeof (args))) { +if (r != -EINTR &©_to_user (argp, &args,sizeof (args))) { r = -EFAULT ;break ; }can you very briefly explain how we can end up with
-EINTRhere?
Quoted 2 lines
do I understand correctly that
-EINTRis possible here only if the process is being killed?
Correct, it would happen if the process has a pending fatal signal (like SIGKILL) in its signal queue.
Quoted 2 lines
[...]
Re: [PATCH v3 3/3] mm: use vma_start_write_killable() in process_vma_walk_lock()
Quoted 38 lines · On Thu, 26 Feb 2026 10:24:49 -0800 Suren Baghdasaryan <[email protected]> wrote:
Quoted 36 lines · Claudio Imbrenda
Quoted 21 lines · On Wed, 25 Feb 2026 23:06:09 -0800 Suren Baghdasaryan <[email protected]> wrote:
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.Signed-off-by Suren Baghdasaryan <[email protected]>--- arch/s390/kvm/kvm-s390.c | 2 +- fs/proc/task_mmu.c | 5 ++++- mm/mempolicy.c | 14 +++++++++++--- mm/pagewalk.c | 20 ++++++++++++++------ mm/vma.c | 22 ++++++++++++++-------- mm/vma.h | 6 ++++++ 6 files changed, 50 insertions(+), 19 deletions(-)arch/s390/kvm/kvm-s390.c +0
-0arch/s390/kvm/kvm-s390.c int kvm_arch_vm_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg) · line 2954
Quoted 8 lines
arch/s390/kvm/kvm-s390.c +1
-1@@ -2948,7 +2948,7 @@ 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 (copy_to_user (argp, &args,sizeof (args))) { +if (r != -EINTR &©_to_user (argp, &args,sizeof (args))) { r = -EFAULT ;break ; }can you very briefly explain how we can end up with
-EINTRhere?
Quoted 8 lines
Quoted 2 lines
do I understand correctly that
-EINTRis possible here only if the process is being killed?Correct, it would happen if the process has a pending fatal signal (like SIGKILL) in its signal queue.
Quoted 2 lines
[...]
Re: [PATCH v3 3/3] mm: use vma_start_write_killable() in process_vma_walk_lock()
In reply to Suren BaghdasaryanQuoted 7 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.Signed-off-by Suren Baghdasaryan <[email protected]>
Have raised concerns below but also this feels like you're trying to do a bit too much in one patch here, probably worth splitting out based on the different parts you changed.
Quoted 14 lines
--- arch/s390/kvm/kvm-s390.c | 2 +- fs/proc/task_mmu.c | 5 ++++- mm/mempolicy.c | 14 +++++++++++--- mm/pagewalk.c | 20 ++++++++++++++------ mm/vma.c | 22 ++++++++++++++-------- mm/vma.h | 6 ++++++ 6 files changed, 50 insertions(+), 19 deletions(-)arch/s390/kvm/kvm-s390.c +0
-0
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 +1
-1@@ -2948,7 +2948,7 @@ 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 (copy_to_user (argp, &args,sizeof (args))) { +if (r != -EINTR &©_to_user (argp, &args,sizeof (args))) {
This is horribly ugly, and if we were already filtering any other instance of -EINTR (if they're even possible from copy_to_user()) why is -EINTR being treated in a special way?
I honestly _hate_ this if (errcode != -EINTR) { ... } pattern in general, I'd really rather we didn't.
It's going to bitrot and people are going to assume it's for some _very good reason_ and nobody will understand why it's getting special treatment...
Surely a fatal signal would have previously resulted in -EFAULT before which is a similar situation so most consistent would be to keep filtering no?
Quoted 16 lines
arch/s390/kvm/kvm-s390.c +0
-0r = -EFAULT ;break ; }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;if (mmap_write_lock_killable (mm)) { count = -EINTR ;
fs/proc/task_mmu.c static ssize_t clear_refs_write(struct file *file, const char __user *buf, · line 1829
Quoted 6 lines
fs/proc/task_mmu.c +2
-1@@ -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 <0 )
Again with this < 0 :) let's be consistent, if (err).
mm/mempolicy.c queue_pages_range(struct mm_struct *mm, unsigned long start, unsigned long end, · line 988
Quoted 10 lines
fs/proc/task_mmu.c +1
-0+ count = err;if (type ==CLEAR_REFS_SOFT_DIRTY ) {mmu_notifier_invalidate_range_end (&range);flush_tlb_mm (mm);mm/mempolicy.c +0
-0@@ -988,6 +988,8 @@ queue_pages_range(struct mm_struct *mm, unsigned long start, unsigned long end, &queue_pages_lock_vma_walk_ops : &queue_pages_walk_ops;
There's a comment above:
* queue_pages_range() may return:
* 0 - all pages already on the right node, or successfully queued for moving
* (or neither strict checking nor moving requested: only range checking).
* >0 - this number of misplaced folios could not be queued for moving
* (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. */
You should add the -EINTR to it.
mm/mempolicy.c queue_pages_range(struct mm_struct *mm, unsigned long start, unsigned long end, · line 992
Quoted 4 lines
mm/mempolicy.c +2
-0err =walk_page_range (mm, start, end, ops, &qp); +if (err == -EINTR ) +return err;
Again, you're special casing without really any justification here. Let's please not special case -EINTR unless you have a _really good_ reason to.
And also - if we fail to walk the page range because we couldn't get a VMA write lock, that's ok. The walk failed. There's nothing to unlock, because we didn't even get the write lock in the first place, so there's no broken state, it's as if we failed at some other point right?
So I don't see why we're special casing this _at all_.
mm/mempolicy.c static long migrate_to_node(struct mm_struct *mm, int source, int dest, · line 1317
Quoted 11 lines
mm/mempolicy.c +2
-0if (!qp.first)/* whole range in hole */ @@ -1309,9 +1311,14 @@ static long migrate_to_node(struct mm_struct *mm, int source, int dest, flags | MPOL_MF_DISCONTIG_OK, &pagelist);mmap_read_unlock (mm); +if (nr_failed == -EINTR ) + err = nr_failed;
Ugh please don't, that's REALLY horrible.
Actually the only way you'd get a write lock happening in the walk_page_range() is if flags & MPOL_MF_WRLOCK, menaing queue_pages_lock_vma_walk_ops are used which specifies .walk_lock = PGWALK_WRLOCK.
And this flag is only set in do_mbind(), not in migrate_to_node().
So this check is actually totally unnecessary. You'll never get -EINTR here.
Maybe this code needs some refactoring though in general... yikes.
mm/mempolicy.c static long migrate_to_node(struct mm_struct *mm, int source, int dest, · line 1321
Quoted 8 lines
mm/mempolicy.c +5
-2+if (!list_empty (&pagelist)) { - err =migrate_pages (&pagelist, alloc_migration_target,NULL , - (unsigned long )&mtc,MIGRATE_SYNC ,MR_SYSCALL ,NULL ); +if (!err) + err =migrate_pages (&pagelist, alloc_migration_target, +NULL , (unsigned long )&mtc, +MIGRATE_SYNC ,MR_SYSCALL ,NULL );
Given the above, this is unnecessary too.
mm/mempolicy.c static long do_mbind(unsigned long start, unsigned long len, · line 1621
Quoted 9 lines
mm/mempolicy.c +0
-0if (err)mm/mempolicy.c +1
-1putback_movable_pages (&pagelist); }@@ -1611,7 +1618,8 @@ static long do_mbind(unsigned long start, unsigned long len, MR_MEMPOLICY_MBIND, NULL); } -if (nr_failed && (flags &MPOL_MF_STRICT )) +/* Do not mask EINTR */
Useless comment... You're not explaining why, and it's obvious what you're doing.
mm/mempolicy.c static long do_mbind(unsigned long start, unsigned long len, · line 1622
Quoted 1 line
mm/mempolicy.c +1
-0+if ((err != -EINTR ) && (nr_failed && (flags &MPOL_MF_STRICT )))
Weird use of parens...
And again why are we treating -EINTR in a special way?
Quoted 89 lines
mm/mempolicy.c +0
-0err = -EIO ;if (!list_empty (&pagelist))putback_movable_pages (&pagelist);mm/pagewalk.c +14
-6@@ -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 +0
-0@@ -998,14 +998,18 @@ static __must_check struct vm_area_struct *vma_merge_existing_range(if (anon_dup)unlink_anon_vmas (anon_dup);
mm/vma.c static __must_check struct vm_area_struct *vma_merge_existing_range( · line 1002
Quoted 10 lines
mm/vma.c +2
-8-/* -* This means we have failed to clone anon_vma's correctly, but no -* actual changes to VMAs have occurred, so no harm no foul - if the -* user doesn't want this reported and instead just wants to give up on -* the merge, allow it. -*/ -if (!vmg->give_up_on_oom) - vmg->state =VMA_MERGE_ERROR_NOMEM ; +if (err == -EINTR ) { + vmg->state =VMA_MERGE_ERROR_INTR ;
Yeah this is incorrect. You seem adament in passing through -EINTR _no matter what_ :)
There are callers that don't care at all if the merge failed, whether through oom or VMA write lock not being acquired.
There's really no benefit in exiting early here I don't think, the subsequent split will call vma_start_write_killable() anyway.
So I think this adds a lot of complexity and mess for nothing.
So can we drop all this change to the merge logic please?
Quoted 49 lines
mm/vma.c +12
-0+ }else { +/* +* This means we have failed to clone anon_vma's correctly, +* but no actual changes to VMAs have occurred, so no harm no +* foul - if the user doesn't want this reported and instead +* just wants to give up on the merge, allow it. +*/ +if (!vmg->give_up_on_oom) + vmg->state =VMA_MERGE_ERROR_NOMEM ; + }return NULL ; }@@ -1681,6 +1685,8 @@ static struct vm_area_struct *vma_modify(struct vma_merge_struct *vmg) merged =vma_merge_existing_range (vmg);if (merged)return merged; +if (vmg_intr (vmg)) +return ERR_PTR (-EINTR );if (vmg_nomem (vmg))return ERR_PTR (-ENOMEM );mm/vma.h +6
-1@@ -56,6 +56,7 @@ struct vma_munmap_struct {enum vma_merge_state {VMA_MERGE_START ,VMA_MERGE_ERROR_NOMEM , +VMA_MERGE_ERROR_INTR ,VMA_MERGE_NOMERGE ,VMA_MERGE_SUCCESS , };@@ -226,6 +227,11 @@ static inline bool vmg_nomem(struct vma_merge_struct *vmg)return vmg->state ==VMA_MERGE_ERROR_NOMEM ; } +static inline bool vmg_intr (struct vma_merge_struct *vmg) +{ +return vmg->state ==VMA_MERGE_ERROR_INTR ; +} +/* Assumes addr >= vma->vm_start. */ static inline pgoff_t vma_pgoff_offset (struct vm_area_struct *vma,unsigned long addr) --2.53.0.414.gf7e9f6c205-goog
Suren Baghdasaryan
reviewerRe: [PATCH v3 3/3] mm: use vma_start_write_killable() in process_vma_walk_lock()
Quoted 38 lines · Lorenzo Stoakes
Quoted 7 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.Signed-off-by Suren Baghdasaryan <[email protected]>Have raised concerns below but also this feels like you're trying to do a bit too much in one patch here, probably worth splitting out based on the different parts you changed.
Quoted 14 lines
--- arch/s390/kvm/kvm-s390.c | 2 +- fs/proc/task_mmu.c | 5 ++++- mm/mempolicy.c | 14 +++++++++++--- mm/pagewalk.c | 20 ++++++++++++++------ mm/vma.c | 22 ++++++++++++++-------- mm/vma.h | 6 ++++++ 6 files changed, 50 insertions(+), 19 deletions(-)arch/s390/kvm/kvm-s390.c +0
-0arch/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 +1
-1@@ -2948,7 +2948,7 @@ 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 (copy_to_user (argp, &args,sizeof (args))) { +if (r != -EINTR &©_to_user (argp, &args,sizeof (args))) {This is horribly ugly, and if we were already filtering any other instance of
-EINTR(if they're even possible fromcopy_to_user()) why is-EINTRbeing treated in a special way?
Quoted 8 lines
I honestly _hate_ this if (errcode !=
-EINTR) { ... } pattern in general, I'd really rather we didn't.It's going to bitrot and people are going to assume it's for some
_verygoodreason_and nobody will understand why it's getting special treatment...Surely a fatal signal would have previously resulted in
-EFAULTbefore which is a similar situation so most consistent would be to keep filtering no?
Current code ignores any error coming from kvm_s390_handle_pv() and proceeds with copy_to_user(), possibly overriding the former error. I don't really know if this is an oversight or an intentional behavior, so I wanted to minimize possible side effects. I guess I should try to fix it properly (or learn why this was done this way). I'll post a separate patch to error out immediately if kvm_s390_handle_pv() fails and will ask s390 experts for review.
Quoted 14 lines
Quoted 14 lines
arch/s390/kvm/kvm-s390.c +0
-0r = -EFAULT ;break ; }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;
Quoted 11 lines
fs/proc/task_mmu.c static ssize_t clear_refs_write(struct file *file, const char __user *buf, · line 1829
Quoted 9 lines
fs/proc/task_mmu.c +2
-1if (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 <0 )Again with this < 0 :) let's be consistent, if (err).
Ack.
Quoted 23 lines
mm/mempolicy.c queue_pages_range(struct mm_struct *mm, unsigned long start, unsigned long end, · line 988
Quoted 11 lines
fs/proc/task_mmu.c +1
-0+ count = err;if (type ==CLEAR_REFS_SOFT_DIRTY ) {mmu_notifier_invalidate_range_end (&range);flush_tlb_mm (mm);mm/mempolicy.c +0
-0@@ -988,6 +988,8 @@ queue_pages_range(struct mm_struct *mm, unsigned long start, unsigned long end, &queue_pages_lock_vma_walk_ops : &queue_pages_walk_ops;There's a comment above:
*
queue_pages_range()may return:
* 0 - all pages already on the right node, or successfully queued for moving
* (or neither strict checking nor moving requested: only range checking).
* >0 - this number of misplaced folios could not be queued for moving
* (a hugetlbfs page or a transparent huge page being counted as 1).
*-EIO- a misplaced page found, whenMPOL_MF_STRICTspecified without MOVEs.
*-EFAULT- a hole in the memory range, whenMPOL_MF_DISCONTIG_OKunspecified. */
Quoted 1 line
You should add the
-EINTRto it.
Ack.
Quoted 9 lines
mm/mempolicy.c queue_pages_range(struct mm_struct *mm, unsigned long start, unsigned long end, · line 992
Quoted 5 lines
mm/mempolicy.c +2
-0err =walk_page_range (mm, start, end, ops, &qp); +if (err == -EINTR ) +return err;Again, you're special casing without really any justification here. Let's please not special case
-EINTRunless you have a_reallygood_reason to.
Quoted 6 lines
And also - if we fail to walk the page range because we couldn't get a VMA write lock, that's ok. The walk failed. There's nothing to unlock, because we didn't even get the write lock in the first place, so there's no broken state, it's as if we failed at some other point right?
So I don't see why we're special casing this
_atall_.
I want to avoid possible -EINTR code override with -EFAULT in the code below. walk_page_range() can return -EINVAL and any other error that ops->pte_hole or ops->test_walk might return. We might be fine treating all of them as -EFAULT but masking -EINTR seems wrong to me. I don't really know a better way to deal with this but if you have a good alternative I would really appreciate it.
Quoted 15 lines
mm/mempolicy.c static long migrate_to_node(struct mm_struct *mm, int source, int dest, · line 1317
Quoted 12 lines
mm/mempolicy.c +2
-0if (!qp.first)/* whole range in hole */ @@ -1309,9 +1311,14 @@ static long migrate_to_node(struct mm_struct *mm, int source, int dest, flags | MPOL_MF_DISCONTIG_OK, &pagelist);mmap_read_unlock (mm); +if (nr_failed == -EINTR ) + err = nr_failed;Ugh please don't, that's REALLY horrible.
Quoted 7 lines
Actually the only way you'd get a write lock happening in the
walk_page_range()is if flags &MPOL_MF_WRLOCK, menaingqueue_pages_lock_vma_walk_opsare used which specifies .walk_lock =PGWALK_WRLOCK.And this flag is only set in
do_mbind(), not inmigrate_to_node().So this check is actually totally unnecessary. You'll never get
-EINTRhere.
Ah, good point. I'll drop this part.
Quoted 2 lines
Maybe this code needs some refactoring though in general... yikes.
Right.
Quoted 11 lines
mm/mempolicy.c static long migrate_to_node(struct mm_struct *mm, int source, int dest, · line 1321
Quoted 9 lines
mm/mempolicy.c +5
-2+if (!list_empty (&pagelist)) { - err =migrate_pages (&pagelist, alloc_migration_target,NULL , - (unsigned long )&mtc,MIGRATE_SYNC ,MR_SYSCALL ,NULL ); +if (!err) + err =migrate_pages (&pagelist, alloc_migration_target, +NULL , (unsigned long )&mtc, +MIGRATE_SYNC ,MR_SYSCALL ,NULL );Given the above, this is unnecessary too.
Ack. Will drop.
Quoted 13 lines
mm/mempolicy.c static long do_mbind(unsigned long start, unsigned long len, · line 1621
Quoted 10 lines
mm/mempolicy.c +0
-0if (err)putback_movable_pages(&pagelist); }mm/mempolicy.c +1
-1@@ -1611,7 +1618,8 @@ static long do_mbind(unsigned long start, unsigned long len, MR_MEMPOLICY_MBIND, NULL); } -if (nr_failed && (flags &MPOL_MF_STRICT )) +/* Do not mask EINTR */ Useless comment... You're not explaining why, and it's obvious what you're doing.
Quoted 5 lines
mm/mempolicy.c static long do_mbind(unsigned long start, unsigned long len, · line 1622
Quoted 1 line
mm/mempolicy.c +1
-0+if ((err != -EINTR ) && (nr_failed && (flags &MPOL_MF_STRICT )))Weird use of parens...
And again why are we treating
-EINTRin a special way?
Ah, actually I don't think I need this here. If queue_pages_range() fails nr_failed gets reset to 0, so the original error won't be masked as -EIO. I'll drop this part.
Quoted 90 lines
Quoted 90 lines
mm/mempolicy.c +0
-0err = -EIO ;if (!list_empty (&pagelist))putback_movable_pages (&pagelist);mm/pagewalk.c +14
-6@@ -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 +0
-0@@ -998,14 +998,18 @@ static __must_check struct vm_area_struct *vma_merge_existing_range(if (anon_dup)unlink_anon_vmas (anon_dup);
Quoted 13 lines
mm/vma.c static __must_check struct vm_area_struct *vma_merge_existing_range( · line 1002
Quoted 10 lines
mm/vma.c +2
-8-/* -* This means we have failed to clone anon_vma's correctly, but no -* actual changes to VMAs have occurred, so no harm no foul - if the -* user doesn't want this reported and instead just wants to give up on -* the merge, allow it. -*/ -if (!vmg->give_up_on_oom) - vmg->state =VMA_MERGE_ERROR_NOMEM ; +if (err == -EINTR ) { + vmg->state =VMA_MERGE_ERROR_INTR ;Yeah this is incorrect. You seem adament in passing through
-EINTR_nomatterwhat_:)
You got me figured out ;)
Quoted 3 lines
There are callers that don't care at all if the merge failed, whether through oom or VMA write lock not being acquired.
Ah, I see. I was a bit puzzled by this vmg->give_up_on_oom flag. I think what you are saying is that errors from vma_merge_existing_range() are ignored unless this flag is set and even then the only possible error is ENOMEM.
Quoted 3 lines
There's really no benefit in exiting early here I don't think, the subsequent split will call
vma_start_write_killable()anyway.
But are we always calling split after the merge?
Quoted 4 lines
So I think this adds a lot of complexity and mess for nothing.
So can we drop all this change to the merge logic please?
Ok but is there a good reason for this unusual error handling logic in vma_merge_existing_range()?
Quoted 51 lines
Quoted 50 lines
mm/vma.c +12
-0+ }else { +/* +* This means we have failed to clone anon_vma's correctly, +* but no actual changes to VMAs have occurred, so no harm no +* foul - if the user doesn't want this reported and instead +* just wants to give up on the merge, allow it. +*/ +if (!vmg->give_up_on_oom) + vmg->state =VMA_MERGE_ERROR_NOMEM ; + }return NULL ; }@@ -1681,6 +1685,8 @@ static struct vm_area_struct *vma_modify(struct vma_merge_struct *vmg) merged =vma_merge_existing_range (vmg);if (merged)return merged; +if (vmg_intr (vmg)) +return ERR_PTR (-EINTR );if (vmg_nomem (vmg))return ERR_PTR (-ENOMEM );mm/vma.h +6
-1@@ -56,6 +56,7 @@ struct vma_munmap_struct {enum vma_merge_state {VMA_MERGE_START ,VMA_MERGE_ERROR_NOMEM , +VMA_MERGE_ERROR_INTR ,VMA_MERGE_NOMERGE ,VMA_MERGE_SUCCESS , };@@ -226,6 +227,11 @@ static inline bool vmg_nomem(struct vma_merge_struct *vmg)return vmg->state ==VMA_MERGE_ERROR_NOMEM ; } +static inline bool vmg_intr (struct vma_merge_struct *vmg) +{ +return vmg->state ==VMA_MERGE_ERROR_INTR ; +} +/* Assumes addr >= vma->vm_start. */ static inline pgoff_t vma_pgoff_offset (struct vm_area_struct *vma,unsigned long addr) --2.53.0.414.gf7e9f6c205-goog
Lorenzo Stoakes (Oracle)
maintainerRe: [PATCH v3 3/3] mm: use vma_start_write_killable() in process_vma_walk_lock()
Quoted 49 lines · Suren Baghdasaryan
Quoted 38 lines · Lorenzo Stoakes
Quoted 7 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.Signed-off-by Suren Baghdasaryan <[email protected]>Have raised concerns below but also this feels like you're trying to do a bit too much in one patch here, probably worth splitting out based on the different parts you changed.
Quoted 14 lines
--- arch/s390/kvm/kvm-s390.c | 2 +- fs/proc/task_mmu.c | 5 ++++- mm/mempolicy.c | 14 +++++++++++--- mm/pagewalk.c | 20 ++++++++++++++------ mm/vma.c | 22 ++++++++++++++-------- mm/vma.h | 6 ++++++ 6 files changed, 50 insertions(+), 19 deletions(-)arch/s390/kvm/kvm-s390.c +0
-0arch/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 +1
-1@@ -2948,7 +2948,7 @@ 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 (copy_to_user (argp, &args,sizeof (args))) { +if (r != -EINTR &©_to_user (argp, &args,sizeof (args))) {This is horribly ugly, and if we were already filtering any other instance of
-EINTR(if they're even possible fromcopy_to_user()) why is-EINTRbeing treated in a special way?Quoted 8 lines
I honestly _hate_ this if (errcode !=
-EINTR) { ... } pattern in general, I'd really rather we didn't.It's going to bitrot and people are going to assume it's for some
_verygoodreason_and nobody will understand why it's getting special treatment...Surely a fatal signal would have previously resulted in
-EFAULTbefore which is a similar situation so most consistent would be to keep filtering no?
Quoted 7 lines
Current code ignores any error coming from
kvm_s390_handle_pv()and proceeds withcopy_to_user(), possibly overriding the former error. I don't really know if this is an oversight or an intentional behavior, so I wanted to minimize possible side effects. I guess I should try to fix it properly (or learn why this was done this way). I'll post a separate patch to error out immediately ifkvm_s390_handle_pv()fails and will ask s390 experts for review.
Thanks!
Quoted 27 lines
Quoted 15 lines
Quoted 15 lines
arch/s390/kvm/kvm-s390.c +0
-0r = -EFAULT ;break ; }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;Quoted 11 lines
fs/proc/task_mmu.c static ssize_t clear_refs_write(struct file *file, const char __user *buf, · line 1829
Quoted 9 lines
fs/proc/task_mmu.c +2
-1if (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 <0 )Again with this < 0 :) let's be consistent, if (err).
Quoted 1 line
Ack.
Thanks!
Quoted 24 lines
Quoted 24 lines
mm/mempolicy.c queue_pages_range(struct mm_struct *mm, unsigned long start, unsigned long end, · line 988
Quoted 12 lines
fs/proc/task_mmu.c +1
-0+ count = err;if (type ==CLEAR_REFS_SOFT_DIRTY ) {mmu_notifier_invalidate_range_end (&range);flush_tlb_mm (mm);mm/mempolicy.c +0
-0@@ -988,6 +988,8 @@ queue_pages_range(struct mm_struct *mm, unsigned long start, unsigned long end, &queue_pages_lock_vma_walk_ops : &queue_pages_walk_ops;There's a comment above:
*
queue_pages_range()may return:
* 0 - all pages already on the right node, or successfully queued for moving
* (or neither strict checking nor moving requested: only range checking).
* >0 - this number of misplaced folios could not be queued for moving
* (a hugetlbfs page or a transparent huge page being counted as 1).
*-EIO- a misplaced page found, whenMPOL_MF_STRICTspecified without MOVEs.
*-EFAULT- a hole in the memory range, whenMPOL_MF_DISCONTIG_OKunspecified. */
Quoted 3 lines
Quoted 1 line
You should add the
-EINTRto it.Ack.
Thanks!
Quoted 15 lines
Quoted 15 lines
mm/mempolicy.c queue_pages_range(struct mm_struct *mm, unsigned long start, unsigned long end, · line 992
Quoted 6 lines
mm/mempolicy.c +2
-0err =walk_page_range (mm, start, end, ops, &qp); +if (err == -EINTR ) +return err;Again, you're special casing without really any justification here. Let's please not special case
-EINTRunless you have a_reallygood_reason to.And also - if we fail to walk the page range because we couldn't get a VMA write lock, that's ok. The walk failed. There's nothing to unlock, because we didn't even get the write lock in the first place, so there's no broken state, it's as if we failed at some other point right?
Quoted 8 lines
Quoted 1 line
So I don't see why we're special casing this
_atall_.I want to avoid possible
-EINTRcode override with-EFAULTin the code below.walk_page_range()can return-EINVALand any other error thatops->pte_holeorops->test_walkmight return. We might be fine treating all of them as-EFAULTbut masking-EINTRseems wrong to me. I don't really know a better way to deal with this but if you have a good alternative I would really appreciate it.
As per Matthew we needn't worry, and in any case if we want to check for fatal signal early exit we can do if (fatal_signal_pending(current)) {} I think?
Quoted 20 lines
Quoted 20 lines
mm/mempolicy.c static long migrate_to_node(struct mm_struct *mm, int source, int dest, · line 1317
Quoted 13 lines
mm/mempolicy.c +2
-0if (!qp.first)/* whole range in hole */ @@ -1309,9 +1311,14 @@ static long migrate_to_node(struct mm_struct *mm, int source, int dest, flags | MPOL_MF_DISCONTIG_OK, &pagelist);mmap_read_unlock (mm); +if (nr_failed == -EINTR ) + err = nr_failed;Ugh please don't, that's REALLY horrible.
Actually the only way you'd get a write lock happening in the
walk_page_range()is if flags &MPOL_MF_WRLOCK, menaingqueue_pages_lock_vma_walk_opsare used which specifies .walk_lock =PGWALK_WRLOCK.
Quoted 5 lines
Quoted 3 lines
And this flag is only set in
do_mbind(), not inmigrate_to_node().So this check is actually totally unnecessary. You'll never get
-EINTRhere.Ah, good point. I'll drop this part.
Thanks!
Quoted 16 lines
Quoted 3 lines
Maybe this code needs some refactoring though in general... yikes.
Right.
Quoted 10 lines
Quoted 9 lines
mm/mempolicy.c +5
-2+if (!list_empty (&pagelist)) { - err =migrate_pages (&pagelist, alloc_migration_target,NULL , - (unsigned long )&mtc,MIGRATE_SYNC ,MR_SYSCALL ,NULL ); +if (!err) + err =migrate_pages (&pagelist, alloc_migration_target, +NULL , (unsigned long )&mtc, +MIGRATE_SYNC ,MR_SYSCALL ,NULL );
Quoted 3 lines
Quoted 1 line
Given the above, this is unnecessary too.
Ack. Will drop.
Thanks!
Quoted 16 lines
Quoted 16 lines
mm/mempolicy.c static long do_mbind(unsigned long start, unsigned long len, · line 1621
Quoted 11 lines
mm/mempolicy.c +0
-0if (err)putback_movable_pages(&pagelist); }mm/mempolicy.c +1
-1@@ -1611,7 +1618,8 @@ static long do_mbind(unsigned long start, unsigned long len, MR_MEMPOLICY_MBIND, NULL); } -if (nr_failed && (flags &MPOL_MF_STRICT )) +/* Do not mask EINTR */ Useless comment... You're not explaining why, and it's obvious what you're doing.
Quoted 1 line
mm/mempolicy.c +1
-0+if ((err != -EINTR ) && (nr_failed && (flags &MPOL_MF_STRICT )))
Quoted 7 lines
Quoted 3 lines
Weird use of parens...
And again why are we treating
-EINTRin a special way?Ah, actually I don't think I need this here. If
queue_pages_range()failsnr_failedgets reset to 0, so the original error won't be masked as-EIO. I'll drop this part.
Thanks!
Quoted 117 lines
Quoted 91 lines
Quoted 91 lines
mm/mempolicy.c +0
-0err = -EIO ;if (!list_empty (&pagelist))putback_movable_pages (&pagelist);mm/pagewalk.c +14
-6@@ -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 +0
-0@@ -998,14 +998,18 @@ static __must_check struct vm_area_struct *vma_merge_existing_range(if (anon_dup)unlink_anon_vmas (anon_dup);Quoted 13 lines
mm/vma.c static __must_check struct vm_area_struct *vma_merge_existing_range( · line 1002
Quoted 10 lines
mm/vma.c +2
-8-/* -* This means we have failed to clone anon_vma's correctly, but no -* actual changes to VMAs have occurred, so no harm no foul - if the -* user doesn't want this reported and instead just wants to give up on -* the merge, allow it. -*/ -if (!vmg->give_up_on_oom) - vmg->state =VMA_MERGE_ERROR_NOMEM ; +if (err == -EINTR ) { + vmg->state =VMA_MERGE_ERROR_INTR ;Yeah this is incorrect. You seem adament in passing through
-EINTR_nomatterwhat_:)You got me figured out ;)
Quoted 3 lines
There are callers that don't care at all if the merge failed, whether through oom or VMA write lock not being acquired.
Ah, I see. I was a bit puzzled by this
vmg->give_up_on_oomflag. I think what you are saying is that errors fromvma_merge_existing_range()are ignored unless this flag is set and even then the only possible error is ENOMEM.Quoted 1 line
Quoted 4 lines
Quoted 2 lines
There's really no benefit in exiting early here I don't think, the subsequent split will call
vma_start_write_killable()anyway.But are we always calling split after the merge?
We wouldn't if start == vma->vm_start and end == vma->vm_end but that'd be a nop anyway :) [in vma_modify(), the only caller].
Quoted 8 lines
Quoted 5 lines
So I think this adds a lot of complexity and mess for nothing.
So can we drop all this change to the merge logic please?
Ok but is there a good reason for this unusual error handling logic in
vma_merge_existing_range()?
It's specifically so we can indicate _why_ the merge didn't succeed, because the function returns NULL. Is checked in vma_modify().
Better this way than an ERR_PTR().
Quoted 40 lines
Quoted 40 lines
Quoted 40 lines
mm/vma.c +12
-0+ }else { +/* +* This means we have failed to clone anon_vma's correctly, +* but no actual changes to VMAs have occurred, so no harm no +* foul - if the user doesn't want this reported and instead +* just wants to give up on the merge, allow it. +*/ +if (!vmg->give_up_on_oom) + vmg->state =VMA_MERGE_ERROR_NOMEM ; + }return NULL ; }@@ -1681,6 +1685,8 @@ static struct vm_area_struct *vma_modify(struct vma_merge_struct *vmg) merged =vma_merge_existing_range (vmg);if (merged)return merged; +if (vmg_intr (vmg)) +return ERR_PTR (-EINTR );if (vmg_nomem (vmg))return ERR_PTR (-ENOMEM );mm/vma.h +1
-0@@ -56,6 +56,7 @@ struct vma_munmap_struct {enum vma_merge_state {VMA_MERGE_START ,VMA_MERGE_ERROR_NOMEM , +VMA_MERGE_ERROR_INTR ,VMA_MERGE_NOMERGE ,VMA_MERGE_SUCCESS , };@@ -226,6 +227,11 @@ static inline bool vmg_nomem(struct vma_merge_struct *vmg)return vmg->state ==VMA_MERGE_ERROR_NOMEM ; }
Quoted 12 lines
Quoted 12 lines
Quoted 11 lines
mm/vma.h +5
-1+static inline bool vmg_intr (struct vma_merge_struct *vmg) +{ +return vmg->state ==VMA_MERGE_ERROR_INTR ; +} +/* Assumes addr >= vma->vm_start. */ static inline pgoff_t vma_pgoff_offset (struct vm_area_struct *vma,unsigned long addr) --2.53.0.414.gf7e9f6c205-goog
Cheers, Lorenzo
Suren Baghdasaryan
reviewerRe: [PATCH v3 3/3] mm: use vma_start_write_killable() in process_vma_walk_lock()
Quoted 146 lines · Lorenzo Stoakes (Oracle)
Quoted 49 lines · Suren Baghdasaryan
Quoted 38 lines · Lorenzo Stoakes
Quoted 7 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.Signed-off-by Suren Baghdasaryan <[email protected]>Have raised concerns below but also this feels like you're trying to do a bit too much in one patch here, probably worth splitting out based on the different parts you changed.
Quoted 14 lines
--- arch/s390/kvm/kvm-s390.c | 2 +- fs/proc/task_mmu.c | 5 ++++- mm/mempolicy.c | 14 +++++++++++--- mm/pagewalk.c | 20 ++++++++++++++------ mm/vma.c | 22 ++++++++++++++-------- mm/vma.h | 6 ++++++ 6 files changed, 50 insertions(+), 19 deletions(-)arch/s390/kvm/kvm-s390.c +0
-0arch/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 +1
-1@@ -2948,7 +2948,7 @@ 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 (copy_to_user (argp, &args,sizeof (args))) { +if (r != -EINTR &©_to_user (argp, &args,sizeof (args))) {This is horribly ugly, and if we were already filtering any other instance of
-EINTR(if they're even possible fromcopy_to_user()) why is-EINTRbeing treated in a special way?Quoted 8 lines
I honestly _hate_ this if (errcode !=
-EINTR) { ... } pattern in general, I'd really rather we didn't.It's going to bitrot and people are going to assume it's for some
_verygoodreason_and nobody will understand why it's getting special treatment...Surely a fatal signal would have previously resulted in
-EFAULTbefore which is a similar situation so most consistent would be to keep filtering no?Quoted 7 lines
Current code ignores any error coming from
kvm_s390_handle_pv()and proceeds withcopy_to_user(), possibly overriding the former error. I don't really know if this is an oversight or an intentional behavior, so I wanted to minimize possible side effects. I guess I should try to fix it properly (or learn why this was done this way). I'll post a separate patch to error out immediately ifkvm_s390_handle_pv()fails and will ask s390 experts for review.Thanks!
Quoted 27 lines
Quoted 15 lines
Quoted 15 lines
arch/s390/kvm/kvm-s390.c +0
-0r = -EFAULT ;break ; }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;Quoted 11 lines
fs/proc/task_mmu.c static ssize_t clear_refs_write(struct file *file, const char __user *buf, · line 1829
Quoted 9 lines
fs/proc/task_mmu.c +2
-1if (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 <0 )Again with this < 0 :) let's be consistent, if (err).
Quoted 1 line
Ack.
Thanks!
Quoted 24 lines
Quoted 24 lines
mm/mempolicy.c queue_pages_range(struct mm_struct *mm, unsigned long start, unsigned long end, · line 988
Quoted 12 lines
fs/proc/task_mmu.c +1
-0+ count = err;if (type ==CLEAR_REFS_SOFT_DIRTY ) {mmu_notifier_invalidate_range_end (&range);flush_tlb_mm (mm);mm/mempolicy.c +0
-0@@ -988,6 +988,8 @@ queue_pages_range(struct mm_struct *mm, unsigned long start, unsigned long end, &queue_pages_lock_vma_walk_ops : &queue_pages_walk_ops;There's a comment above:
*
queue_pages_range()may return:
* 0 - all pages already on the right node, or successfully queued for moving
* (or neither strict checking nor moving requested: only range checking).
* >0 - this number of misplaced folios could not be queued for moving
* (a hugetlbfs page or a transparent huge page being counted as 1).
*-EIO- a misplaced page found, whenMPOL_MF_STRICTspecified without MOVEs.
*-EFAULT- a hole in the memory range, whenMPOL_MF_DISCONTIG_OKunspecified. */Quoted 3 lines
Quoted 1 line
You should add the
-EINTRto it.Ack.
Thanks!
Quoted 15 lines
Quoted 15 lines
mm/mempolicy.c queue_pages_range(struct mm_struct *mm, unsigned long start, unsigned long end, · line 992
Quoted 6 lines
mm/mempolicy.c +2
-0err =walk_page_range (mm, start, end, ops, &qp); +if (err == -EINTR ) +return err;Again, you're special casing without really any justification here. Let's please not special case
-EINTRunless you have a_reallygood_reason to.And also - if we fail to walk the page range because we couldn't get a VMA write lock, that's ok. The walk failed. There's nothing to unlock, because we didn't even get the write lock in the first place, so there's no broken state, it's as if we failed at some other point right?
Quoted 8 lines
Quoted 1 line
So I don't see why we're special casing this
_atall_.I want to avoid possible
-EINTRcode override with-EFAULTin the code below.walk_page_range()can return-EINVALand any other error thatops->pte_holeorops->test_walkmight return. We might be fine treating all of them as-EFAULTbut masking-EINTRseems wrong to me. I don't really know a better way to deal with this but if you have a good alternative I would really appreciate it.
Quoted 2 lines
As per Matthew we needn't worry, and in any case if we want to check for fatal signal early exit we can do if (
fatal_signal_pending(current)) {} I think?
Ok, fatal_signal_pending() seems like a better alternative. Thanks!
Quoted 194 lines
Quoted 21 lines
Quoted 21 lines
Quoted 7 lines
mm/mempolicy.c +0
-0if (!qp.first)/* whole range in hole */ mm/mempolicy.c static long migrate_to_node(struct mm_struct *mm, int source, int dest, · line 1317
Quoted 7 lines
mm/mempolicy.c +2
-0@@ -1309,9 +1311,14 @@ static long migrate_to_node(struct mm_struct *mm, int source, int dest, flags | MPOL_MF_DISCONTIG_OK, &pagelist);mmap_read_unlock (mm); +if (nr_failed == -EINTR ) + err = nr_failed;Ugh please don't, that's REALLY horrible.
Actually the only way you'd get a write lock happening in the
walk_page_range()is if flags &MPOL_MF_WRLOCK, menaingqueue_pages_lock_vma_walk_opsare used which specifies .walk_lock =PGWALK_WRLOCK.Quoted 5 lines
Quoted 3 lines
And this flag is only set in
do_mbind(), not inmigrate_to_node().So this check is actually totally unnecessary. You'll never get
-EINTRhere.Ah, good point. I'll drop this part.
Thanks!
Quoted 16 lines
Quoted 3 lines
Maybe this code needs some refactoring though in general... yikes.
Right.
Quoted 10 lines
Quoted 9 lines
mm/mempolicy.c +5
-2+if (!list_empty (&pagelist)) { - err =migrate_pages (&pagelist, alloc_migration_target,NULL , - (unsigned long )&mtc,MIGRATE_SYNC ,MR_SYSCALL ,NULL ); +if (!err) + err =migrate_pages (&pagelist, alloc_migration_target, +NULL , (unsigned long )&mtc, +MIGRATE_SYNC ,MR_SYSCALL ,NULL );Quoted 3 lines
Quoted 1 line
Given the above, this is unnecessary too.
Ack. Will drop.
Thanks!
Quoted 16 lines
Quoted 16 lines
mm/mempolicy.c static long do_mbind(unsigned long start, unsigned long len, · line 1621
Quoted 11 lines
mm/mempolicy.c +0
-0if (err)putback_movable_pages(&pagelist); }mm/mempolicy.c +1
-1@@ -1611,7 +1618,8 @@ static long do_mbind(unsigned long start, unsigned long len, MR_MEMPOLICY_MBIND, NULL); } -if (nr_failed && (flags &MPOL_MF_STRICT )) +/* Do not mask EINTR */ Useless comment... You're not explaining why, and it's obvious what you're doing.
Quoted 1 line
mm/mempolicy.c +1
-0+if ((err != -EINTR ) && (nr_failed && (flags &MPOL_MF_STRICT )))Quoted 7 lines
Quoted 3 lines
Weird use of parens...
And again why are we treating
-EINTRin a special way?Ah, actually I don't think I need this here. If
queue_pages_range()failsnr_failedgets reset to 0, so the original error won't be masked as-EIO. I'll drop this part.Thanks!
Quoted 117 lines
Quoted 91 lines
Quoted 91 lines
mm/mempolicy.c +0
-0err = -EIO ;if (!list_empty (&pagelist))putback_movable_pages (&pagelist);mm/pagewalk.c +14
-6@@ -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 +0
-0@@ -998,14 +998,18 @@ static __must_check struct vm_area_struct *vma_merge_existing_range(if (anon_dup)unlink_anon_vmas (anon_dup);Quoted 13 lines
mm/vma.c static __must_check struct vm_area_struct *vma_merge_existing_range( · line 1002
Quoted 10 lines
mm/vma.c +2
-8-/* -* This means we have failed to clone anon_vma's correctly, but no -* actual changes to VMAs have occurred, so no harm no foul - if the -* user doesn't want this reported and instead just wants to give up on -* the merge, allow it. -*/ -if (!vmg->give_up_on_oom) - vmg->state =VMA_MERGE_ERROR_NOMEM ; +if (err == -EINTR ) { + vmg->state =VMA_MERGE_ERROR_INTR ;Yeah this is incorrect. You seem adament in passing through
-EINTR_nomatterwhat_:)You got me figured out ;)
Quoted 3 lines
There are callers that don't care at all if the merge failed, whether through oom or VMA write lock not being acquired.
Ah, I see. I was a bit puzzled by this
vmg->give_up_on_oomflag. I think what you are saying is that errors fromvma_merge_existing_range()are ignored unless this flag is set and even then the only possible error is ENOMEM.Quoted 1 line
Quoted 7 lines
Quoted 4 lines
Quoted 2 lines
There's really no benefit in exiting early here I don't think, the subsequent split will call
vma_start_write_killable()anyway.But are we always calling split after the merge?
We wouldn't if start ==
vma->vm_startand end ==vma->vm_endbut that'd be a nop anyway :) [invma_modify(), the only caller].
I see. Ok, then this is indeed an unnecessary complexity with no benefit. I'll drop this part.
Quoted 7 lines
Quoted 7 lines
Quoted 6 lines
So I think this adds a lot of complexity and mess for nothing.
So can we drop all this change to the merge logic please?
Quoted 7 lines
Quoted 2 lines
Ok but is there a good reason for this unusual error handling logic in
vma_merge_existing_range()?It's specifically so we can indicate _why_ the merge didn't succeed, because the function returns NULL. Is checked in
vma_modify().Better this way than an
ERR_PTR().
I think ERR_PTR() would be a more usual pattern for such cases but I guess either way works fine.
Thanks!
Quoted 56 lines
Quoted 42 lines
Quoted 42 lines
Quoted 42 lines
mm/vma.c +12
-0+ }else { +/* +* This means we have failed to clone anon_vma's correctly, +* but no actual changes to VMAs have occurred, so no harm no +* foul - if the user doesn't want this reported and instead +* just wants to give up on the merge, allow it. +*/ +if (!vmg->give_up_on_oom) + vmg->state =VMA_MERGE_ERROR_NOMEM ; + }return NULL ; }@@ -1681,6 +1685,8 @@ static struct vm_area_struct *vma_modify(struct vma_merge_struct *vmg) merged =vma_merge_existing_range (vmg);if (merged)return merged; +if (vmg_intr (vmg)) +return ERR_PTR (-EINTR );if (vmg_nomem (vmg))return ERR_PTR (-ENOMEM );mm/vma.h +1
-0@@ -56,6 +56,7 @@ struct vma_munmap_struct {enum vma_merge_state {VMA_MERGE_START ,VMA_MERGE_ERROR_NOMEM , +VMA_MERGE_ERROR_INTR ,VMA_MERGE_NOMERGE ,VMA_MERGE_SUCCESS , };@@ -226,6 +227,11 @@ static inline bool vmg_nomem(struct vma_merge_struct *vmg)return vmg->state ==VMA_MERGE_ERROR_NOMEM ; }Quoted 12 lines
Quoted 12 lines
Quoted 11 lines
mm/vma.h +5
-1+static inline bool vmg_intr (struct vma_merge_struct *vmg) +{ +return vmg->state ==VMA_MERGE_ERROR_INTR ; +} +/* Assumes addr >= vma->vm_start. */ static inline pgoff_t vma_pgoff_offset (struct vm_area_struct *vma,unsigned long addr) --2.53.0.414.gf7e9f6c205-goog
Cheers, Lorenzo