1*49b1b8d6SLorenzo Stoakes // SPDX-License-Identifier: GPL-2.0-or-later 2*49b1b8d6SLorenzo Stoakes 3*49b1b8d6SLorenzo Stoakes /* 4*49b1b8d6SLorenzo Stoakes * VMA-specific functions. 5*49b1b8d6SLorenzo Stoakes */ 6*49b1b8d6SLorenzo Stoakes 7*49b1b8d6SLorenzo Stoakes #include "vma_internal.h" 8*49b1b8d6SLorenzo Stoakes #include "vma.h" 9*49b1b8d6SLorenzo Stoakes 10*49b1b8d6SLorenzo Stoakes /* 11*49b1b8d6SLorenzo Stoakes * If the vma has a ->close operation then the driver probably needs to release 12*49b1b8d6SLorenzo Stoakes * per-vma resources, so we don't attempt to merge those if the caller indicates 13*49b1b8d6SLorenzo Stoakes * the current vma may be removed as part of the merge. 14*49b1b8d6SLorenzo Stoakes */ 15*49b1b8d6SLorenzo Stoakes static inline bool is_mergeable_vma(struct vm_area_struct *vma, 16*49b1b8d6SLorenzo Stoakes struct file *file, unsigned long vm_flags, 17*49b1b8d6SLorenzo Stoakes struct vm_userfaultfd_ctx vm_userfaultfd_ctx, 18*49b1b8d6SLorenzo Stoakes struct anon_vma_name *anon_name, bool may_remove_vma) 19*49b1b8d6SLorenzo Stoakes { 20*49b1b8d6SLorenzo Stoakes /* 21*49b1b8d6SLorenzo Stoakes * VM_SOFTDIRTY should not prevent from VMA merging, if we 22*49b1b8d6SLorenzo Stoakes * match the flags but dirty bit -- the caller should mark 23*49b1b8d6SLorenzo Stoakes * merged VMA as dirty. If dirty bit won't be excluded from 24*49b1b8d6SLorenzo Stoakes * comparison, we increase pressure on the memory system forcing 25*49b1b8d6SLorenzo Stoakes * the kernel to generate new VMAs when old one could be 26*49b1b8d6SLorenzo Stoakes * extended instead. 27*49b1b8d6SLorenzo Stoakes */ 28*49b1b8d6SLorenzo Stoakes if ((vma->vm_flags ^ vm_flags) & ~VM_SOFTDIRTY) 29*49b1b8d6SLorenzo Stoakes return false; 30*49b1b8d6SLorenzo Stoakes if (vma->vm_file != file) 31*49b1b8d6SLorenzo Stoakes return false; 32*49b1b8d6SLorenzo Stoakes if (may_remove_vma && vma->vm_ops && vma->vm_ops->close) 33*49b1b8d6SLorenzo Stoakes return false; 34*49b1b8d6SLorenzo Stoakes if (!is_mergeable_vm_userfaultfd_ctx(vma, vm_userfaultfd_ctx)) 35*49b1b8d6SLorenzo Stoakes return false; 36*49b1b8d6SLorenzo Stoakes if (!anon_vma_name_eq(anon_vma_name(vma), anon_name)) 37*49b1b8d6SLorenzo Stoakes return false; 38*49b1b8d6SLorenzo Stoakes return true; 39*49b1b8d6SLorenzo Stoakes } 40*49b1b8d6SLorenzo Stoakes 41*49b1b8d6SLorenzo Stoakes static inline bool is_mergeable_anon_vma(struct anon_vma *anon_vma1, 42*49b1b8d6SLorenzo Stoakes struct anon_vma *anon_vma2, struct vm_area_struct *vma) 43*49b1b8d6SLorenzo Stoakes { 44*49b1b8d6SLorenzo Stoakes /* 45*49b1b8d6SLorenzo Stoakes * The list_is_singular() test is to avoid merging VMA cloned from 46*49b1b8d6SLorenzo Stoakes * parents. This can improve scalability caused by anon_vma lock. 47*49b1b8d6SLorenzo Stoakes */ 48*49b1b8d6SLorenzo Stoakes if ((!anon_vma1 || !anon_vma2) && (!vma || 49*49b1b8d6SLorenzo Stoakes list_is_singular(&vma->anon_vma_chain))) 50*49b1b8d6SLorenzo Stoakes return true; 51*49b1b8d6SLorenzo Stoakes return anon_vma1 == anon_vma2; 52*49b1b8d6SLorenzo Stoakes } 53*49b1b8d6SLorenzo Stoakes 54*49b1b8d6SLorenzo Stoakes /* 55*49b1b8d6SLorenzo Stoakes * init_multi_vma_prep() - Initializer for struct vma_prepare 56*49b1b8d6SLorenzo Stoakes * @vp: The vma_prepare struct 57*49b1b8d6SLorenzo Stoakes * @vma: The vma that will be altered once locked 58*49b1b8d6SLorenzo Stoakes * @next: The next vma if it is to be adjusted 59*49b1b8d6SLorenzo Stoakes * @remove: The first vma to be removed 60*49b1b8d6SLorenzo Stoakes * @remove2: The second vma to be removed 61*49b1b8d6SLorenzo Stoakes */ 62*49b1b8d6SLorenzo Stoakes static void init_multi_vma_prep(struct vma_prepare *vp, 63*49b1b8d6SLorenzo Stoakes struct vm_area_struct *vma, 64*49b1b8d6SLorenzo Stoakes struct vm_area_struct *next, 65*49b1b8d6SLorenzo Stoakes struct vm_area_struct *remove, 66*49b1b8d6SLorenzo Stoakes struct vm_area_struct *remove2) 67*49b1b8d6SLorenzo Stoakes { 68*49b1b8d6SLorenzo Stoakes memset(vp, 0, sizeof(struct vma_prepare)); 69*49b1b8d6SLorenzo Stoakes vp->vma = vma; 70*49b1b8d6SLorenzo Stoakes vp->anon_vma = vma->anon_vma; 71*49b1b8d6SLorenzo Stoakes vp->remove = remove; 72*49b1b8d6SLorenzo Stoakes vp->remove2 = remove2; 73*49b1b8d6SLorenzo Stoakes vp->adj_next = next; 74*49b1b8d6SLorenzo Stoakes if (!vp->anon_vma && next) 75*49b1b8d6SLorenzo Stoakes vp->anon_vma = next->anon_vma; 76*49b1b8d6SLorenzo Stoakes 77*49b1b8d6SLorenzo Stoakes vp->file = vma->vm_file; 78*49b1b8d6SLorenzo Stoakes if (vp->file) 79*49b1b8d6SLorenzo Stoakes vp->mapping = vma->vm_file->f_mapping; 80*49b1b8d6SLorenzo Stoakes 81*49b1b8d6SLorenzo Stoakes } 82*49b1b8d6SLorenzo Stoakes 83*49b1b8d6SLorenzo Stoakes /* 84*49b1b8d6SLorenzo Stoakes * Return true if we can merge this (vm_flags,anon_vma,file,vm_pgoff) 85*49b1b8d6SLorenzo Stoakes * in front of (at a lower virtual address and file offset than) the vma. 86*49b1b8d6SLorenzo Stoakes * 87*49b1b8d6SLorenzo Stoakes * We cannot merge two vmas if they have differently assigned (non-NULL) 88*49b1b8d6SLorenzo Stoakes * anon_vmas, nor if same anon_vma is assigned but offsets incompatible. 89*49b1b8d6SLorenzo Stoakes * 90*49b1b8d6SLorenzo Stoakes * We don't check here for the merged mmap wrapping around the end of pagecache 91*49b1b8d6SLorenzo Stoakes * indices (16TB on ia32) because do_mmap() does not permit mmap's which 92*49b1b8d6SLorenzo Stoakes * wrap, nor mmaps which cover the final page at index -1UL. 93*49b1b8d6SLorenzo Stoakes * 94*49b1b8d6SLorenzo Stoakes * We assume the vma may be removed as part of the merge. 95*49b1b8d6SLorenzo Stoakes */ 96*49b1b8d6SLorenzo Stoakes bool 97*49b1b8d6SLorenzo Stoakes can_vma_merge_before(struct vm_area_struct *vma, unsigned long vm_flags, 98*49b1b8d6SLorenzo Stoakes struct anon_vma *anon_vma, struct file *file, 99*49b1b8d6SLorenzo Stoakes pgoff_t vm_pgoff, struct vm_userfaultfd_ctx vm_userfaultfd_ctx, 100*49b1b8d6SLorenzo Stoakes struct anon_vma_name *anon_name) 101*49b1b8d6SLorenzo Stoakes { 102*49b1b8d6SLorenzo Stoakes if (is_mergeable_vma(vma, file, vm_flags, vm_userfaultfd_ctx, anon_name, true) && 103*49b1b8d6SLorenzo Stoakes is_mergeable_anon_vma(anon_vma, vma->anon_vma, vma)) { 104*49b1b8d6SLorenzo Stoakes if (vma->vm_pgoff == vm_pgoff) 105*49b1b8d6SLorenzo Stoakes return true; 106*49b1b8d6SLorenzo Stoakes } 107*49b1b8d6SLorenzo Stoakes return false; 108*49b1b8d6SLorenzo Stoakes } 109*49b1b8d6SLorenzo Stoakes 110*49b1b8d6SLorenzo Stoakes /* 111*49b1b8d6SLorenzo Stoakes * Return true if we can merge this (vm_flags,anon_vma,file,vm_pgoff) 112*49b1b8d6SLorenzo Stoakes * beyond (at a higher virtual address and file offset than) the vma. 113*49b1b8d6SLorenzo Stoakes * 114*49b1b8d6SLorenzo Stoakes * We cannot merge two vmas if they have differently assigned (non-NULL) 115*49b1b8d6SLorenzo Stoakes * anon_vmas, nor if same anon_vma is assigned but offsets incompatible. 116*49b1b8d6SLorenzo Stoakes * 117*49b1b8d6SLorenzo Stoakes * We assume that vma is not removed as part of the merge. 118*49b1b8d6SLorenzo Stoakes */ 119*49b1b8d6SLorenzo Stoakes bool 120*49b1b8d6SLorenzo Stoakes can_vma_merge_after(struct vm_area_struct *vma, unsigned long vm_flags, 121*49b1b8d6SLorenzo Stoakes struct anon_vma *anon_vma, struct file *file, 122*49b1b8d6SLorenzo Stoakes pgoff_t vm_pgoff, struct vm_userfaultfd_ctx vm_userfaultfd_ctx, 123*49b1b8d6SLorenzo Stoakes struct anon_vma_name *anon_name) 124*49b1b8d6SLorenzo Stoakes { 125*49b1b8d6SLorenzo Stoakes if (is_mergeable_vma(vma, file, vm_flags, vm_userfaultfd_ctx, anon_name, false) && 126*49b1b8d6SLorenzo Stoakes is_mergeable_anon_vma(anon_vma, vma->anon_vma, vma)) { 127*49b1b8d6SLorenzo Stoakes pgoff_t vm_pglen; 128*49b1b8d6SLorenzo Stoakes 129*49b1b8d6SLorenzo Stoakes vm_pglen = vma_pages(vma); 130*49b1b8d6SLorenzo Stoakes if (vma->vm_pgoff + vm_pglen == vm_pgoff) 131*49b1b8d6SLorenzo Stoakes return true; 132*49b1b8d6SLorenzo Stoakes } 133*49b1b8d6SLorenzo Stoakes return false; 134*49b1b8d6SLorenzo Stoakes } 135*49b1b8d6SLorenzo Stoakes 136*49b1b8d6SLorenzo Stoakes /* 137*49b1b8d6SLorenzo Stoakes * Close a vm structure and free it. 138*49b1b8d6SLorenzo Stoakes */ 139*49b1b8d6SLorenzo Stoakes void remove_vma(struct vm_area_struct *vma, bool unreachable) 140*49b1b8d6SLorenzo Stoakes { 141*49b1b8d6SLorenzo Stoakes might_sleep(); 142*49b1b8d6SLorenzo Stoakes if (vma->vm_ops && vma->vm_ops->close) 143*49b1b8d6SLorenzo Stoakes vma->vm_ops->close(vma); 144*49b1b8d6SLorenzo Stoakes if (vma->vm_file) 145*49b1b8d6SLorenzo Stoakes fput(vma->vm_file); 146*49b1b8d6SLorenzo Stoakes mpol_put(vma_policy(vma)); 147*49b1b8d6SLorenzo Stoakes if (unreachable) 148*49b1b8d6SLorenzo Stoakes __vm_area_free(vma); 149*49b1b8d6SLorenzo Stoakes else 150*49b1b8d6SLorenzo Stoakes vm_area_free(vma); 151*49b1b8d6SLorenzo Stoakes } 152*49b1b8d6SLorenzo Stoakes 153*49b1b8d6SLorenzo Stoakes /* 154*49b1b8d6SLorenzo Stoakes * Get rid of page table information in the indicated region. 155*49b1b8d6SLorenzo Stoakes * 156*49b1b8d6SLorenzo Stoakes * Called with the mm semaphore held. 157*49b1b8d6SLorenzo Stoakes */ 158*49b1b8d6SLorenzo Stoakes void unmap_region(struct mm_struct *mm, struct ma_state *mas, 159*49b1b8d6SLorenzo Stoakes struct vm_area_struct *vma, struct vm_area_struct *prev, 160*49b1b8d6SLorenzo Stoakes struct vm_area_struct *next, unsigned long start, 161*49b1b8d6SLorenzo Stoakes unsigned long end, unsigned long tree_end, bool mm_wr_locked) 162*49b1b8d6SLorenzo Stoakes { 163*49b1b8d6SLorenzo Stoakes struct mmu_gather tlb; 164*49b1b8d6SLorenzo Stoakes unsigned long mt_start = mas->index; 165*49b1b8d6SLorenzo Stoakes 166*49b1b8d6SLorenzo Stoakes lru_add_drain(); 167*49b1b8d6SLorenzo Stoakes tlb_gather_mmu(&tlb, mm); 168*49b1b8d6SLorenzo Stoakes update_hiwater_rss(mm); 169*49b1b8d6SLorenzo Stoakes unmap_vmas(&tlb, mas, vma, start, end, tree_end, mm_wr_locked); 170*49b1b8d6SLorenzo Stoakes mas_set(mas, mt_start); 171*49b1b8d6SLorenzo Stoakes free_pgtables(&tlb, mas, vma, prev ? prev->vm_end : FIRST_USER_ADDRESS, 172*49b1b8d6SLorenzo Stoakes next ? next->vm_start : USER_PGTABLES_CEILING, 173*49b1b8d6SLorenzo Stoakes mm_wr_locked); 174*49b1b8d6SLorenzo Stoakes tlb_finish_mmu(&tlb); 175*49b1b8d6SLorenzo Stoakes } 176*49b1b8d6SLorenzo Stoakes 177*49b1b8d6SLorenzo Stoakes /* 178*49b1b8d6SLorenzo Stoakes * __split_vma() bypasses sysctl_max_map_count checking. We use this where it 179*49b1b8d6SLorenzo Stoakes * has already been checked or doesn't make sense to fail. 180*49b1b8d6SLorenzo Stoakes * VMA Iterator will point to the end VMA. 181*49b1b8d6SLorenzo Stoakes */ 182*49b1b8d6SLorenzo Stoakes static int __split_vma(struct vma_iterator *vmi, struct vm_area_struct *vma, 183*49b1b8d6SLorenzo Stoakes unsigned long addr, int new_below) 184*49b1b8d6SLorenzo Stoakes { 185*49b1b8d6SLorenzo Stoakes struct vma_prepare vp; 186*49b1b8d6SLorenzo Stoakes struct vm_area_struct *new; 187*49b1b8d6SLorenzo Stoakes int err; 188*49b1b8d6SLorenzo Stoakes 189*49b1b8d6SLorenzo Stoakes WARN_ON(vma->vm_start >= addr); 190*49b1b8d6SLorenzo Stoakes WARN_ON(vma->vm_end <= addr); 191*49b1b8d6SLorenzo Stoakes 192*49b1b8d6SLorenzo Stoakes if (vma->vm_ops && vma->vm_ops->may_split) { 193*49b1b8d6SLorenzo Stoakes err = vma->vm_ops->may_split(vma, addr); 194*49b1b8d6SLorenzo Stoakes if (err) 195*49b1b8d6SLorenzo Stoakes return err; 196*49b1b8d6SLorenzo Stoakes } 197*49b1b8d6SLorenzo Stoakes 198*49b1b8d6SLorenzo Stoakes new = vm_area_dup(vma); 199*49b1b8d6SLorenzo Stoakes if (!new) 200*49b1b8d6SLorenzo Stoakes return -ENOMEM; 201*49b1b8d6SLorenzo Stoakes 202*49b1b8d6SLorenzo Stoakes if (new_below) { 203*49b1b8d6SLorenzo Stoakes new->vm_end = addr; 204*49b1b8d6SLorenzo Stoakes } else { 205*49b1b8d6SLorenzo Stoakes new->vm_start = addr; 206*49b1b8d6SLorenzo Stoakes new->vm_pgoff += ((addr - vma->vm_start) >> PAGE_SHIFT); 207*49b1b8d6SLorenzo Stoakes } 208*49b1b8d6SLorenzo Stoakes 209*49b1b8d6SLorenzo Stoakes err = -ENOMEM; 210*49b1b8d6SLorenzo Stoakes vma_iter_config(vmi, new->vm_start, new->vm_end); 211*49b1b8d6SLorenzo Stoakes if (vma_iter_prealloc(vmi, new)) 212*49b1b8d6SLorenzo Stoakes goto out_free_vma; 213*49b1b8d6SLorenzo Stoakes 214*49b1b8d6SLorenzo Stoakes err = vma_dup_policy(vma, new); 215*49b1b8d6SLorenzo Stoakes if (err) 216*49b1b8d6SLorenzo Stoakes goto out_free_vmi; 217*49b1b8d6SLorenzo Stoakes 218*49b1b8d6SLorenzo Stoakes err = anon_vma_clone(new, vma); 219*49b1b8d6SLorenzo Stoakes if (err) 220*49b1b8d6SLorenzo Stoakes goto out_free_mpol; 221*49b1b8d6SLorenzo Stoakes 222*49b1b8d6SLorenzo Stoakes if (new->vm_file) 223*49b1b8d6SLorenzo Stoakes get_file(new->vm_file); 224*49b1b8d6SLorenzo Stoakes 225*49b1b8d6SLorenzo Stoakes if (new->vm_ops && new->vm_ops->open) 226*49b1b8d6SLorenzo Stoakes new->vm_ops->open(new); 227*49b1b8d6SLorenzo Stoakes 228*49b1b8d6SLorenzo Stoakes vma_start_write(vma); 229*49b1b8d6SLorenzo Stoakes vma_start_write(new); 230*49b1b8d6SLorenzo Stoakes 231*49b1b8d6SLorenzo Stoakes init_vma_prep(&vp, vma); 232*49b1b8d6SLorenzo Stoakes vp.insert = new; 233*49b1b8d6SLorenzo Stoakes vma_prepare(&vp); 234*49b1b8d6SLorenzo Stoakes vma_adjust_trans_huge(vma, vma->vm_start, addr, 0); 235*49b1b8d6SLorenzo Stoakes 236*49b1b8d6SLorenzo Stoakes if (new_below) { 237*49b1b8d6SLorenzo Stoakes vma->vm_start = addr; 238*49b1b8d6SLorenzo Stoakes vma->vm_pgoff += (addr - new->vm_start) >> PAGE_SHIFT; 239*49b1b8d6SLorenzo Stoakes } else { 240*49b1b8d6SLorenzo Stoakes vma->vm_end = addr; 241*49b1b8d6SLorenzo Stoakes } 242*49b1b8d6SLorenzo Stoakes 243*49b1b8d6SLorenzo Stoakes /* vma_complete stores the new vma */ 244*49b1b8d6SLorenzo Stoakes vma_complete(&vp, vmi, vma->vm_mm); 245*49b1b8d6SLorenzo Stoakes 246*49b1b8d6SLorenzo Stoakes /* Success. */ 247*49b1b8d6SLorenzo Stoakes if (new_below) 248*49b1b8d6SLorenzo Stoakes vma_next(vmi); 249*49b1b8d6SLorenzo Stoakes return 0; 250*49b1b8d6SLorenzo Stoakes 251*49b1b8d6SLorenzo Stoakes out_free_mpol: 252*49b1b8d6SLorenzo Stoakes mpol_put(vma_policy(new)); 253*49b1b8d6SLorenzo Stoakes out_free_vmi: 254*49b1b8d6SLorenzo Stoakes vma_iter_free(vmi); 255*49b1b8d6SLorenzo Stoakes out_free_vma: 256*49b1b8d6SLorenzo Stoakes vm_area_free(new); 257*49b1b8d6SLorenzo Stoakes return err; 258*49b1b8d6SLorenzo Stoakes } 259*49b1b8d6SLorenzo Stoakes 260*49b1b8d6SLorenzo Stoakes /* 261*49b1b8d6SLorenzo Stoakes * Split a vma into two pieces at address 'addr', a new vma is allocated 262*49b1b8d6SLorenzo Stoakes * either for the first part or the tail. 263*49b1b8d6SLorenzo Stoakes */ 264*49b1b8d6SLorenzo Stoakes static int split_vma(struct vma_iterator *vmi, struct vm_area_struct *vma, 265*49b1b8d6SLorenzo Stoakes unsigned long addr, int new_below) 266*49b1b8d6SLorenzo Stoakes { 267*49b1b8d6SLorenzo Stoakes if (vma->vm_mm->map_count >= sysctl_max_map_count) 268*49b1b8d6SLorenzo Stoakes return -ENOMEM; 269*49b1b8d6SLorenzo Stoakes 270*49b1b8d6SLorenzo Stoakes return __split_vma(vmi, vma, addr, new_below); 271*49b1b8d6SLorenzo Stoakes } 272*49b1b8d6SLorenzo Stoakes 273*49b1b8d6SLorenzo Stoakes /* 274*49b1b8d6SLorenzo Stoakes * Ok - we have the memory areas we should free on a maple tree so release them, 275*49b1b8d6SLorenzo Stoakes * and do the vma updates. 276*49b1b8d6SLorenzo Stoakes * 277*49b1b8d6SLorenzo Stoakes * Called with the mm semaphore held. 278*49b1b8d6SLorenzo Stoakes */ 279*49b1b8d6SLorenzo Stoakes static inline void remove_mt(struct mm_struct *mm, struct ma_state *mas) 280*49b1b8d6SLorenzo Stoakes { 281*49b1b8d6SLorenzo Stoakes unsigned long nr_accounted = 0; 282*49b1b8d6SLorenzo Stoakes struct vm_area_struct *vma; 283*49b1b8d6SLorenzo Stoakes 284*49b1b8d6SLorenzo Stoakes /* Update high watermark before we lower total_vm */ 285*49b1b8d6SLorenzo Stoakes update_hiwater_vm(mm); 286*49b1b8d6SLorenzo Stoakes mas_for_each(mas, vma, ULONG_MAX) { 287*49b1b8d6SLorenzo Stoakes long nrpages = vma_pages(vma); 288*49b1b8d6SLorenzo Stoakes 289*49b1b8d6SLorenzo Stoakes if (vma->vm_flags & VM_ACCOUNT) 290*49b1b8d6SLorenzo Stoakes nr_accounted += nrpages; 291*49b1b8d6SLorenzo Stoakes vm_stat_account(mm, vma->vm_flags, -nrpages); 292*49b1b8d6SLorenzo Stoakes remove_vma(vma, false); 293*49b1b8d6SLorenzo Stoakes } 294*49b1b8d6SLorenzo Stoakes vm_unacct_memory(nr_accounted); 295*49b1b8d6SLorenzo Stoakes } 296*49b1b8d6SLorenzo Stoakes 297*49b1b8d6SLorenzo Stoakes /* 298*49b1b8d6SLorenzo Stoakes * init_vma_prep() - Initializer wrapper for vma_prepare struct 299*49b1b8d6SLorenzo Stoakes * @vp: The vma_prepare struct 300*49b1b8d6SLorenzo Stoakes * @vma: The vma that will be altered once locked 301*49b1b8d6SLorenzo Stoakes */ 302*49b1b8d6SLorenzo Stoakes void init_vma_prep(struct vma_prepare *vp, 303*49b1b8d6SLorenzo Stoakes struct vm_area_struct *vma) 304*49b1b8d6SLorenzo Stoakes { 305*49b1b8d6SLorenzo Stoakes init_multi_vma_prep(vp, vma, NULL, NULL, NULL); 306*49b1b8d6SLorenzo Stoakes } 307*49b1b8d6SLorenzo Stoakes 308*49b1b8d6SLorenzo Stoakes /* 309*49b1b8d6SLorenzo Stoakes * Requires inode->i_mapping->i_mmap_rwsem 310*49b1b8d6SLorenzo Stoakes */ 311*49b1b8d6SLorenzo Stoakes static void __remove_shared_vm_struct(struct vm_area_struct *vma, 312*49b1b8d6SLorenzo Stoakes struct address_space *mapping) 313*49b1b8d6SLorenzo Stoakes { 314*49b1b8d6SLorenzo Stoakes if (vma_is_shared_maywrite(vma)) 315*49b1b8d6SLorenzo Stoakes mapping_unmap_writable(mapping); 316*49b1b8d6SLorenzo Stoakes 317*49b1b8d6SLorenzo Stoakes flush_dcache_mmap_lock(mapping); 318*49b1b8d6SLorenzo Stoakes vma_interval_tree_remove(vma, &mapping->i_mmap); 319*49b1b8d6SLorenzo Stoakes flush_dcache_mmap_unlock(mapping); 320*49b1b8d6SLorenzo Stoakes } 321*49b1b8d6SLorenzo Stoakes 322*49b1b8d6SLorenzo Stoakes /* 323*49b1b8d6SLorenzo Stoakes * vma has some anon_vma assigned, and is already inserted on that 324*49b1b8d6SLorenzo Stoakes * anon_vma's interval trees. 325*49b1b8d6SLorenzo Stoakes * 326*49b1b8d6SLorenzo Stoakes * Before updating the vma's vm_start / vm_end / vm_pgoff fields, the 327*49b1b8d6SLorenzo Stoakes * vma must be removed from the anon_vma's interval trees using 328*49b1b8d6SLorenzo Stoakes * anon_vma_interval_tree_pre_update_vma(). 329*49b1b8d6SLorenzo Stoakes * 330*49b1b8d6SLorenzo Stoakes * After the update, the vma will be reinserted using 331*49b1b8d6SLorenzo Stoakes * anon_vma_interval_tree_post_update_vma(). 332*49b1b8d6SLorenzo Stoakes * 333*49b1b8d6SLorenzo Stoakes * The entire update must be protected by exclusive mmap_lock and by 334*49b1b8d6SLorenzo Stoakes * the root anon_vma's mutex. 335*49b1b8d6SLorenzo Stoakes */ 336*49b1b8d6SLorenzo Stoakes void 337*49b1b8d6SLorenzo Stoakes anon_vma_interval_tree_pre_update_vma(struct vm_area_struct *vma) 338*49b1b8d6SLorenzo Stoakes { 339*49b1b8d6SLorenzo Stoakes struct anon_vma_chain *avc; 340*49b1b8d6SLorenzo Stoakes 341*49b1b8d6SLorenzo Stoakes list_for_each_entry(avc, &vma->anon_vma_chain, same_vma) 342*49b1b8d6SLorenzo Stoakes anon_vma_interval_tree_remove(avc, &avc->anon_vma->rb_root); 343*49b1b8d6SLorenzo Stoakes } 344*49b1b8d6SLorenzo Stoakes 345*49b1b8d6SLorenzo Stoakes void 346*49b1b8d6SLorenzo Stoakes anon_vma_interval_tree_post_update_vma(struct vm_area_struct *vma) 347*49b1b8d6SLorenzo Stoakes { 348*49b1b8d6SLorenzo Stoakes struct anon_vma_chain *avc; 349*49b1b8d6SLorenzo Stoakes 350*49b1b8d6SLorenzo Stoakes list_for_each_entry(avc, &vma->anon_vma_chain, same_vma) 351*49b1b8d6SLorenzo Stoakes anon_vma_interval_tree_insert(avc, &avc->anon_vma->rb_root); 352*49b1b8d6SLorenzo Stoakes } 353*49b1b8d6SLorenzo Stoakes 354*49b1b8d6SLorenzo Stoakes static void __vma_link_file(struct vm_area_struct *vma, 355*49b1b8d6SLorenzo Stoakes struct address_space *mapping) 356*49b1b8d6SLorenzo Stoakes { 357*49b1b8d6SLorenzo Stoakes if (vma_is_shared_maywrite(vma)) 358*49b1b8d6SLorenzo Stoakes mapping_allow_writable(mapping); 359*49b1b8d6SLorenzo Stoakes 360*49b1b8d6SLorenzo Stoakes flush_dcache_mmap_lock(mapping); 361*49b1b8d6SLorenzo Stoakes vma_interval_tree_insert(vma, &mapping->i_mmap); 362*49b1b8d6SLorenzo Stoakes flush_dcache_mmap_unlock(mapping); 363*49b1b8d6SLorenzo Stoakes } 364*49b1b8d6SLorenzo Stoakes 365*49b1b8d6SLorenzo Stoakes /* 366*49b1b8d6SLorenzo Stoakes * vma_prepare() - Helper function for handling locking VMAs prior to altering 367*49b1b8d6SLorenzo Stoakes * @vp: The initialized vma_prepare struct 368*49b1b8d6SLorenzo Stoakes */ 369*49b1b8d6SLorenzo Stoakes void vma_prepare(struct vma_prepare *vp) 370*49b1b8d6SLorenzo Stoakes { 371*49b1b8d6SLorenzo Stoakes if (vp->file) { 372*49b1b8d6SLorenzo Stoakes uprobe_munmap(vp->vma, vp->vma->vm_start, vp->vma->vm_end); 373*49b1b8d6SLorenzo Stoakes 374*49b1b8d6SLorenzo Stoakes if (vp->adj_next) 375*49b1b8d6SLorenzo Stoakes uprobe_munmap(vp->adj_next, vp->adj_next->vm_start, 376*49b1b8d6SLorenzo Stoakes vp->adj_next->vm_end); 377*49b1b8d6SLorenzo Stoakes 378*49b1b8d6SLorenzo Stoakes i_mmap_lock_write(vp->mapping); 379*49b1b8d6SLorenzo Stoakes if (vp->insert && vp->insert->vm_file) { 380*49b1b8d6SLorenzo Stoakes /* 381*49b1b8d6SLorenzo Stoakes * Put into interval tree now, so instantiated pages 382*49b1b8d6SLorenzo Stoakes * are visible to arm/parisc __flush_dcache_page 383*49b1b8d6SLorenzo Stoakes * throughout; but we cannot insert into address 384*49b1b8d6SLorenzo Stoakes * space until vma start or end is updated. 385*49b1b8d6SLorenzo Stoakes */ 386*49b1b8d6SLorenzo Stoakes __vma_link_file(vp->insert, 387*49b1b8d6SLorenzo Stoakes vp->insert->vm_file->f_mapping); 388*49b1b8d6SLorenzo Stoakes } 389*49b1b8d6SLorenzo Stoakes } 390*49b1b8d6SLorenzo Stoakes 391*49b1b8d6SLorenzo Stoakes if (vp->anon_vma) { 392*49b1b8d6SLorenzo Stoakes anon_vma_lock_write(vp->anon_vma); 393*49b1b8d6SLorenzo Stoakes anon_vma_interval_tree_pre_update_vma(vp->vma); 394*49b1b8d6SLorenzo Stoakes if (vp->adj_next) 395*49b1b8d6SLorenzo Stoakes anon_vma_interval_tree_pre_update_vma(vp->adj_next); 396*49b1b8d6SLorenzo Stoakes } 397*49b1b8d6SLorenzo Stoakes 398*49b1b8d6SLorenzo Stoakes if (vp->file) { 399*49b1b8d6SLorenzo Stoakes flush_dcache_mmap_lock(vp->mapping); 400*49b1b8d6SLorenzo Stoakes vma_interval_tree_remove(vp->vma, &vp->mapping->i_mmap); 401*49b1b8d6SLorenzo Stoakes if (vp->adj_next) 402*49b1b8d6SLorenzo Stoakes vma_interval_tree_remove(vp->adj_next, 403*49b1b8d6SLorenzo Stoakes &vp->mapping->i_mmap); 404*49b1b8d6SLorenzo Stoakes } 405*49b1b8d6SLorenzo Stoakes 406*49b1b8d6SLorenzo Stoakes } 407*49b1b8d6SLorenzo Stoakes 408*49b1b8d6SLorenzo Stoakes /* 409*49b1b8d6SLorenzo Stoakes * dup_anon_vma() - Helper function to duplicate anon_vma 410*49b1b8d6SLorenzo Stoakes * @dst: The destination VMA 411*49b1b8d6SLorenzo Stoakes * @src: The source VMA 412*49b1b8d6SLorenzo Stoakes * @dup: Pointer to the destination VMA when successful. 413*49b1b8d6SLorenzo Stoakes * 414*49b1b8d6SLorenzo Stoakes * Returns: 0 on success. 415*49b1b8d6SLorenzo Stoakes */ 416*49b1b8d6SLorenzo Stoakes static int dup_anon_vma(struct vm_area_struct *dst, 417*49b1b8d6SLorenzo Stoakes struct vm_area_struct *src, struct vm_area_struct **dup) 418*49b1b8d6SLorenzo Stoakes { 419*49b1b8d6SLorenzo Stoakes /* 420*49b1b8d6SLorenzo Stoakes * Easily overlooked: when mprotect shifts the boundary, make sure the 421*49b1b8d6SLorenzo Stoakes * expanding vma has anon_vma set if the shrinking vma had, to cover any 422*49b1b8d6SLorenzo Stoakes * anon pages imported. 423*49b1b8d6SLorenzo Stoakes */ 424*49b1b8d6SLorenzo Stoakes if (src->anon_vma && !dst->anon_vma) { 425*49b1b8d6SLorenzo Stoakes int ret; 426*49b1b8d6SLorenzo Stoakes 427*49b1b8d6SLorenzo Stoakes vma_assert_write_locked(dst); 428*49b1b8d6SLorenzo Stoakes dst->anon_vma = src->anon_vma; 429*49b1b8d6SLorenzo Stoakes ret = anon_vma_clone(dst, src); 430*49b1b8d6SLorenzo Stoakes if (ret) 431*49b1b8d6SLorenzo Stoakes return ret; 432*49b1b8d6SLorenzo Stoakes 433*49b1b8d6SLorenzo Stoakes *dup = dst; 434*49b1b8d6SLorenzo Stoakes } 435*49b1b8d6SLorenzo Stoakes 436*49b1b8d6SLorenzo Stoakes return 0; 437*49b1b8d6SLorenzo Stoakes } 438*49b1b8d6SLorenzo Stoakes 439*49b1b8d6SLorenzo Stoakes #ifdef CONFIG_DEBUG_VM_MAPLE_TREE 440*49b1b8d6SLorenzo Stoakes void validate_mm(struct mm_struct *mm) 441*49b1b8d6SLorenzo Stoakes { 442*49b1b8d6SLorenzo Stoakes int bug = 0; 443*49b1b8d6SLorenzo Stoakes int i = 0; 444*49b1b8d6SLorenzo Stoakes struct vm_area_struct *vma; 445*49b1b8d6SLorenzo Stoakes VMA_ITERATOR(vmi, mm, 0); 446*49b1b8d6SLorenzo Stoakes 447*49b1b8d6SLorenzo Stoakes mt_validate(&mm->mm_mt); 448*49b1b8d6SLorenzo Stoakes for_each_vma(vmi, vma) { 449*49b1b8d6SLorenzo Stoakes #ifdef CONFIG_DEBUG_VM_RB 450*49b1b8d6SLorenzo Stoakes struct anon_vma *anon_vma = vma->anon_vma; 451*49b1b8d6SLorenzo Stoakes struct anon_vma_chain *avc; 452*49b1b8d6SLorenzo Stoakes #endif 453*49b1b8d6SLorenzo Stoakes unsigned long vmi_start, vmi_end; 454*49b1b8d6SLorenzo Stoakes bool warn = 0; 455*49b1b8d6SLorenzo Stoakes 456*49b1b8d6SLorenzo Stoakes vmi_start = vma_iter_addr(&vmi); 457*49b1b8d6SLorenzo Stoakes vmi_end = vma_iter_end(&vmi); 458*49b1b8d6SLorenzo Stoakes if (VM_WARN_ON_ONCE_MM(vma->vm_end != vmi_end, mm)) 459*49b1b8d6SLorenzo Stoakes warn = 1; 460*49b1b8d6SLorenzo Stoakes 461*49b1b8d6SLorenzo Stoakes if (VM_WARN_ON_ONCE_MM(vma->vm_start != vmi_start, mm)) 462*49b1b8d6SLorenzo Stoakes warn = 1; 463*49b1b8d6SLorenzo Stoakes 464*49b1b8d6SLorenzo Stoakes if (warn) { 465*49b1b8d6SLorenzo Stoakes pr_emerg("issue in %s\n", current->comm); 466*49b1b8d6SLorenzo Stoakes dump_stack(); 467*49b1b8d6SLorenzo Stoakes dump_vma(vma); 468*49b1b8d6SLorenzo Stoakes pr_emerg("tree range: %px start %lx end %lx\n", vma, 469*49b1b8d6SLorenzo Stoakes vmi_start, vmi_end - 1); 470*49b1b8d6SLorenzo Stoakes vma_iter_dump_tree(&vmi); 471*49b1b8d6SLorenzo Stoakes } 472*49b1b8d6SLorenzo Stoakes 473*49b1b8d6SLorenzo Stoakes #ifdef CONFIG_DEBUG_VM_RB 474*49b1b8d6SLorenzo Stoakes if (anon_vma) { 475*49b1b8d6SLorenzo Stoakes anon_vma_lock_read(anon_vma); 476*49b1b8d6SLorenzo Stoakes list_for_each_entry(avc, &vma->anon_vma_chain, same_vma) 477*49b1b8d6SLorenzo Stoakes anon_vma_interval_tree_verify(avc); 478*49b1b8d6SLorenzo Stoakes anon_vma_unlock_read(anon_vma); 479*49b1b8d6SLorenzo Stoakes } 480*49b1b8d6SLorenzo Stoakes #endif 481*49b1b8d6SLorenzo Stoakes i++; 482*49b1b8d6SLorenzo Stoakes } 483*49b1b8d6SLorenzo Stoakes if (i != mm->map_count) { 484*49b1b8d6SLorenzo Stoakes pr_emerg("map_count %d vma iterator %d\n", mm->map_count, i); 485*49b1b8d6SLorenzo Stoakes bug = 1; 486*49b1b8d6SLorenzo Stoakes } 487*49b1b8d6SLorenzo Stoakes VM_BUG_ON_MM(bug, mm); 488*49b1b8d6SLorenzo Stoakes } 489*49b1b8d6SLorenzo Stoakes #endif /* CONFIG_DEBUG_VM_MAPLE_TREE */ 490*49b1b8d6SLorenzo Stoakes 491*49b1b8d6SLorenzo Stoakes /* 492*49b1b8d6SLorenzo Stoakes * vma_expand - Expand an existing VMA 493*49b1b8d6SLorenzo Stoakes * 494*49b1b8d6SLorenzo Stoakes * @vmi: The vma iterator 495*49b1b8d6SLorenzo Stoakes * @vma: The vma to expand 496*49b1b8d6SLorenzo Stoakes * @start: The start of the vma 497*49b1b8d6SLorenzo Stoakes * @end: The exclusive end of the vma 498*49b1b8d6SLorenzo Stoakes * @pgoff: The page offset of vma 499*49b1b8d6SLorenzo Stoakes * @next: The current of next vma. 500*49b1b8d6SLorenzo Stoakes * 501*49b1b8d6SLorenzo Stoakes * Expand @vma to @start and @end. Can expand off the start and end. Will 502*49b1b8d6SLorenzo Stoakes * expand over @next if it's different from @vma and @end == @next->vm_end. 503*49b1b8d6SLorenzo Stoakes * Checking if the @vma can expand and merge with @next needs to be handled by 504*49b1b8d6SLorenzo Stoakes * the caller. 505*49b1b8d6SLorenzo Stoakes * 506*49b1b8d6SLorenzo Stoakes * Returns: 0 on success 507*49b1b8d6SLorenzo Stoakes */ 508*49b1b8d6SLorenzo Stoakes int vma_expand(struct vma_iterator *vmi, struct vm_area_struct *vma, 509*49b1b8d6SLorenzo Stoakes unsigned long start, unsigned long end, pgoff_t pgoff, 510*49b1b8d6SLorenzo Stoakes struct vm_area_struct *next) 511*49b1b8d6SLorenzo Stoakes { 512*49b1b8d6SLorenzo Stoakes struct vm_area_struct *anon_dup = NULL; 513*49b1b8d6SLorenzo Stoakes bool remove_next = false; 514*49b1b8d6SLorenzo Stoakes struct vma_prepare vp; 515*49b1b8d6SLorenzo Stoakes 516*49b1b8d6SLorenzo Stoakes vma_start_write(vma); 517*49b1b8d6SLorenzo Stoakes if (next && (vma != next) && (end == next->vm_end)) { 518*49b1b8d6SLorenzo Stoakes int ret; 519*49b1b8d6SLorenzo Stoakes 520*49b1b8d6SLorenzo Stoakes remove_next = true; 521*49b1b8d6SLorenzo Stoakes vma_start_write(next); 522*49b1b8d6SLorenzo Stoakes ret = dup_anon_vma(vma, next, &anon_dup); 523*49b1b8d6SLorenzo Stoakes if (ret) 524*49b1b8d6SLorenzo Stoakes return ret; 525*49b1b8d6SLorenzo Stoakes } 526*49b1b8d6SLorenzo Stoakes 527*49b1b8d6SLorenzo Stoakes init_multi_vma_prep(&vp, vma, NULL, remove_next ? next : NULL, NULL); 528*49b1b8d6SLorenzo Stoakes /* Not merging but overwriting any part of next is not handled. */ 529*49b1b8d6SLorenzo Stoakes VM_WARN_ON(next && !vp.remove && 530*49b1b8d6SLorenzo Stoakes next != vma && end > next->vm_start); 531*49b1b8d6SLorenzo Stoakes /* Only handles expanding */ 532*49b1b8d6SLorenzo Stoakes VM_WARN_ON(vma->vm_start < start || vma->vm_end > end); 533*49b1b8d6SLorenzo Stoakes 534*49b1b8d6SLorenzo Stoakes /* Note: vma iterator must be pointing to 'start' */ 535*49b1b8d6SLorenzo Stoakes vma_iter_config(vmi, start, end); 536*49b1b8d6SLorenzo Stoakes if (vma_iter_prealloc(vmi, vma)) 537*49b1b8d6SLorenzo Stoakes goto nomem; 538*49b1b8d6SLorenzo Stoakes 539*49b1b8d6SLorenzo Stoakes vma_prepare(&vp); 540*49b1b8d6SLorenzo Stoakes vma_adjust_trans_huge(vma, start, end, 0); 541*49b1b8d6SLorenzo Stoakes vma_set_range(vma, start, end, pgoff); 542*49b1b8d6SLorenzo Stoakes vma_iter_store(vmi, vma); 543*49b1b8d6SLorenzo Stoakes 544*49b1b8d6SLorenzo Stoakes vma_complete(&vp, vmi, vma->vm_mm); 545*49b1b8d6SLorenzo Stoakes return 0; 546*49b1b8d6SLorenzo Stoakes 547*49b1b8d6SLorenzo Stoakes nomem: 548*49b1b8d6SLorenzo Stoakes if (anon_dup) 549*49b1b8d6SLorenzo Stoakes unlink_anon_vmas(anon_dup); 550*49b1b8d6SLorenzo Stoakes return -ENOMEM; 551*49b1b8d6SLorenzo Stoakes } 552*49b1b8d6SLorenzo Stoakes 553*49b1b8d6SLorenzo Stoakes /* 554*49b1b8d6SLorenzo Stoakes * vma_shrink() - Reduce an existing VMAs memory area 555*49b1b8d6SLorenzo Stoakes * @vmi: The vma iterator 556*49b1b8d6SLorenzo Stoakes * @vma: The VMA to modify 557*49b1b8d6SLorenzo Stoakes * @start: The new start 558*49b1b8d6SLorenzo Stoakes * @end: The new end 559*49b1b8d6SLorenzo Stoakes * 560*49b1b8d6SLorenzo Stoakes * Returns: 0 on success, -ENOMEM otherwise 561*49b1b8d6SLorenzo Stoakes */ 562*49b1b8d6SLorenzo Stoakes int vma_shrink(struct vma_iterator *vmi, struct vm_area_struct *vma, 563*49b1b8d6SLorenzo Stoakes unsigned long start, unsigned long end, pgoff_t pgoff) 564*49b1b8d6SLorenzo Stoakes { 565*49b1b8d6SLorenzo Stoakes struct vma_prepare vp; 566*49b1b8d6SLorenzo Stoakes 567*49b1b8d6SLorenzo Stoakes WARN_ON((vma->vm_start != start) && (vma->vm_end != end)); 568*49b1b8d6SLorenzo Stoakes 569*49b1b8d6SLorenzo Stoakes if (vma->vm_start < start) 570*49b1b8d6SLorenzo Stoakes vma_iter_config(vmi, vma->vm_start, start); 571*49b1b8d6SLorenzo Stoakes else 572*49b1b8d6SLorenzo Stoakes vma_iter_config(vmi, end, vma->vm_end); 573*49b1b8d6SLorenzo Stoakes 574*49b1b8d6SLorenzo Stoakes if (vma_iter_prealloc(vmi, NULL)) 575*49b1b8d6SLorenzo Stoakes return -ENOMEM; 576*49b1b8d6SLorenzo Stoakes 577*49b1b8d6SLorenzo Stoakes vma_start_write(vma); 578*49b1b8d6SLorenzo Stoakes 579*49b1b8d6SLorenzo Stoakes init_vma_prep(&vp, vma); 580*49b1b8d6SLorenzo Stoakes vma_prepare(&vp); 581*49b1b8d6SLorenzo Stoakes vma_adjust_trans_huge(vma, start, end, 0); 582*49b1b8d6SLorenzo Stoakes 583*49b1b8d6SLorenzo Stoakes vma_iter_clear(vmi); 584*49b1b8d6SLorenzo Stoakes vma_set_range(vma, start, end, pgoff); 585*49b1b8d6SLorenzo Stoakes vma_complete(&vp, vmi, vma->vm_mm); 586*49b1b8d6SLorenzo Stoakes return 0; 587*49b1b8d6SLorenzo Stoakes } 588*49b1b8d6SLorenzo Stoakes 589*49b1b8d6SLorenzo Stoakes /* 590*49b1b8d6SLorenzo Stoakes * vma_complete- Helper function for handling the unlocking after altering VMAs, 591*49b1b8d6SLorenzo Stoakes * or for inserting a VMA. 592*49b1b8d6SLorenzo Stoakes * 593*49b1b8d6SLorenzo Stoakes * @vp: The vma_prepare struct 594*49b1b8d6SLorenzo Stoakes * @vmi: The vma iterator 595*49b1b8d6SLorenzo Stoakes * @mm: The mm_struct 596*49b1b8d6SLorenzo Stoakes */ 597*49b1b8d6SLorenzo Stoakes void vma_complete(struct vma_prepare *vp, 598*49b1b8d6SLorenzo Stoakes struct vma_iterator *vmi, struct mm_struct *mm) 599*49b1b8d6SLorenzo Stoakes { 600*49b1b8d6SLorenzo Stoakes if (vp->file) { 601*49b1b8d6SLorenzo Stoakes if (vp->adj_next) 602*49b1b8d6SLorenzo Stoakes vma_interval_tree_insert(vp->adj_next, 603*49b1b8d6SLorenzo Stoakes &vp->mapping->i_mmap); 604*49b1b8d6SLorenzo Stoakes vma_interval_tree_insert(vp->vma, &vp->mapping->i_mmap); 605*49b1b8d6SLorenzo Stoakes flush_dcache_mmap_unlock(vp->mapping); 606*49b1b8d6SLorenzo Stoakes } 607*49b1b8d6SLorenzo Stoakes 608*49b1b8d6SLorenzo Stoakes if (vp->remove && vp->file) { 609*49b1b8d6SLorenzo Stoakes __remove_shared_vm_struct(vp->remove, vp->mapping); 610*49b1b8d6SLorenzo Stoakes if (vp->remove2) 611*49b1b8d6SLorenzo Stoakes __remove_shared_vm_struct(vp->remove2, vp->mapping); 612*49b1b8d6SLorenzo Stoakes } else if (vp->insert) { 613*49b1b8d6SLorenzo Stoakes /* 614*49b1b8d6SLorenzo Stoakes * split_vma has split insert from vma, and needs 615*49b1b8d6SLorenzo Stoakes * us to insert it before dropping the locks 616*49b1b8d6SLorenzo Stoakes * (it may either follow vma or precede it). 617*49b1b8d6SLorenzo Stoakes */ 618*49b1b8d6SLorenzo Stoakes vma_iter_store(vmi, vp->insert); 619*49b1b8d6SLorenzo Stoakes mm->map_count++; 620*49b1b8d6SLorenzo Stoakes } 621*49b1b8d6SLorenzo Stoakes 622*49b1b8d6SLorenzo Stoakes if (vp->anon_vma) { 623*49b1b8d6SLorenzo Stoakes anon_vma_interval_tree_post_update_vma(vp->vma); 624*49b1b8d6SLorenzo Stoakes if (vp->adj_next) 625*49b1b8d6SLorenzo Stoakes anon_vma_interval_tree_post_update_vma(vp->adj_next); 626*49b1b8d6SLorenzo Stoakes anon_vma_unlock_write(vp->anon_vma); 627*49b1b8d6SLorenzo Stoakes } 628*49b1b8d6SLorenzo Stoakes 629*49b1b8d6SLorenzo Stoakes if (vp->file) { 630*49b1b8d6SLorenzo Stoakes i_mmap_unlock_write(vp->mapping); 631*49b1b8d6SLorenzo Stoakes uprobe_mmap(vp->vma); 632*49b1b8d6SLorenzo Stoakes 633*49b1b8d6SLorenzo Stoakes if (vp->adj_next) 634*49b1b8d6SLorenzo Stoakes uprobe_mmap(vp->adj_next); 635*49b1b8d6SLorenzo Stoakes } 636*49b1b8d6SLorenzo Stoakes 637*49b1b8d6SLorenzo Stoakes if (vp->remove) { 638*49b1b8d6SLorenzo Stoakes again: 639*49b1b8d6SLorenzo Stoakes vma_mark_detached(vp->remove, true); 640*49b1b8d6SLorenzo Stoakes if (vp->file) { 641*49b1b8d6SLorenzo Stoakes uprobe_munmap(vp->remove, vp->remove->vm_start, 642*49b1b8d6SLorenzo Stoakes vp->remove->vm_end); 643*49b1b8d6SLorenzo Stoakes fput(vp->file); 644*49b1b8d6SLorenzo Stoakes } 645*49b1b8d6SLorenzo Stoakes if (vp->remove->anon_vma) 646*49b1b8d6SLorenzo Stoakes anon_vma_merge(vp->vma, vp->remove); 647*49b1b8d6SLorenzo Stoakes mm->map_count--; 648*49b1b8d6SLorenzo Stoakes mpol_put(vma_policy(vp->remove)); 649*49b1b8d6SLorenzo Stoakes if (!vp->remove2) 650*49b1b8d6SLorenzo Stoakes WARN_ON_ONCE(vp->vma->vm_end < vp->remove->vm_end); 651*49b1b8d6SLorenzo Stoakes vm_area_free(vp->remove); 652*49b1b8d6SLorenzo Stoakes 653*49b1b8d6SLorenzo Stoakes /* 654*49b1b8d6SLorenzo Stoakes * In mprotect's case 6 (see comments on vma_merge), 655*49b1b8d6SLorenzo Stoakes * we are removing both mid and next vmas 656*49b1b8d6SLorenzo Stoakes */ 657*49b1b8d6SLorenzo Stoakes if (vp->remove2) { 658*49b1b8d6SLorenzo Stoakes vp->remove = vp->remove2; 659*49b1b8d6SLorenzo Stoakes vp->remove2 = NULL; 660*49b1b8d6SLorenzo Stoakes goto again; 661*49b1b8d6SLorenzo Stoakes } 662*49b1b8d6SLorenzo Stoakes } 663*49b1b8d6SLorenzo Stoakes if (vp->insert && vp->file) 664*49b1b8d6SLorenzo Stoakes uprobe_mmap(vp->insert); 665*49b1b8d6SLorenzo Stoakes validate_mm(mm); 666*49b1b8d6SLorenzo Stoakes } 667*49b1b8d6SLorenzo Stoakes 668*49b1b8d6SLorenzo Stoakes /* 669*49b1b8d6SLorenzo Stoakes * do_vmi_align_munmap() - munmap the aligned region from @start to @end. 670*49b1b8d6SLorenzo Stoakes * @vmi: The vma iterator 671*49b1b8d6SLorenzo Stoakes * @vma: The starting vm_area_struct 672*49b1b8d6SLorenzo Stoakes * @mm: The mm_struct 673*49b1b8d6SLorenzo Stoakes * @start: The aligned start address to munmap. 674*49b1b8d6SLorenzo Stoakes * @end: The aligned end address to munmap. 675*49b1b8d6SLorenzo Stoakes * @uf: The userfaultfd list_head 676*49b1b8d6SLorenzo Stoakes * @unlock: Set to true to drop the mmap_lock. unlocking only happens on 677*49b1b8d6SLorenzo Stoakes * success. 678*49b1b8d6SLorenzo Stoakes * 679*49b1b8d6SLorenzo Stoakes * Return: 0 on success and drops the lock if so directed, error and leaves the 680*49b1b8d6SLorenzo Stoakes * lock held otherwise. 681*49b1b8d6SLorenzo Stoakes */ 682*49b1b8d6SLorenzo Stoakes int 683*49b1b8d6SLorenzo Stoakes do_vmi_align_munmap(struct vma_iterator *vmi, struct vm_area_struct *vma, 684*49b1b8d6SLorenzo Stoakes struct mm_struct *mm, unsigned long start, 685*49b1b8d6SLorenzo Stoakes unsigned long end, struct list_head *uf, bool unlock) 686*49b1b8d6SLorenzo Stoakes { 687*49b1b8d6SLorenzo Stoakes struct vm_area_struct *prev, *next = NULL; 688*49b1b8d6SLorenzo Stoakes struct maple_tree mt_detach; 689*49b1b8d6SLorenzo Stoakes int count = 0; 690*49b1b8d6SLorenzo Stoakes int error = -ENOMEM; 691*49b1b8d6SLorenzo Stoakes unsigned long locked_vm = 0; 692*49b1b8d6SLorenzo Stoakes MA_STATE(mas_detach, &mt_detach, 0, 0); 693*49b1b8d6SLorenzo Stoakes mt_init_flags(&mt_detach, vmi->mas.tree->ma_flags & MT_FLAGS_LOCK_MASK); 694*49b1b8d6SLorenzo Stoakes mt_on_stack(mt_detach); 695*49b1b8d6SLorenzo Stoakes 696*49b1b8d6SLorenzo Stoakes /* 697*49b1b8d6SLorenzo Stoakes * If we need to split any vma, do it now to save pain later. 698*49b1b8d6SLorenzo Stoakes * 699*49b1b8d6SLorenzo Stoakes * Note: mremap's move_vma VM_ACCOUNT handling assumes a partially 700*49b1b8d6SLorenzo Stoakes * unmapped vm_area_struct will remain in use: so lower split_vma 701*49b1b8d6SLorenzo Stoakes * places tmp vma above, and higher split_vma places tmp vma below. 702*49b1b8d6SLorenzo Stoakes */ 703*49b1b8d6SLorenzo Stoakes 704*49b1b8d6SLorenzo Stoakes /* Does it split the first one? */ 705*49b1b8d6SLorenzo Stoakes if (start > vma->vm_start) { 706*49b1b8d6SLorenzo Stoakes 707*49b1b8d6SLorenzo Stoakes /* 708*49b1b8d6SLorenzo Stoakes * Make sure that map_count on return from munmap() will 709*49b1b8d6SLorenzo Stoakes * not exceed its limit; but let map_count go just above 710*49b1b8d6SLorenzo Stoakes * its limit temporarily, to help free resources as expected. 711*49b1b8d6SLorenzo Stoakes */ 712*49b1b8d6SLorenzo Stoakes if (end < vma->vm_end && mm->map_count >= sysctl_max_map_count) 713*49b1b8d6SLorenzo Stoakes goto map_count_exceeded; 714*49b1b8d6SLorenzo Stoakes 715*49b1b8d6SLorenzo Stoakes error = __split_vma(vmi, vma, start, 1); 716*49b1b8d6SLorenzo Stoakes if (error) 717*49b1b8d6SLorenzo Stoakes goto start_split_failed; 718*49b1b8d6SLorenzo Stoakes } 719*49b1b8d6SLorenzo Stoakes 720*49b1b8d6SLorenzo Stoakes /* 721*49b1b8d6SLorenzo Stoakes * Detach a range of VMAs from the mm. Using next as a temp variable as 722*49b1b8d6SLorenzo Stoakes * it is always overwritten. 723*49b1b8d6SLorenzo Stoakes */ 724*49b1b8d6SLorenzo Stoakes next = vma; 725*49b1b8d6SLorenzo Stoakes do { 726*49b1b8d6SLorenzo Stoakes /* Does it split the end? */ 727*49b1b8d6SLorenzo Stoakes if (next->vm_end > end) { 728*49b1b8d6SLorenzo Stoakes error = __split_vma(vmi, next, end, 0); 729*49b1b8d6SLorenzo Stoakes if (error) 730*49b1b8d6SLorenzo Stoakes goto end_split_failed; 731*49b1b8d6SLorenzo Stoakes } 732*49b1b8d6SLorenzo Stoakes vma_start_write(next); 733*49b1b8d6SLorenzo Stoakes mas_set(&mas_detach, count); 734*49b1b8d6SLorenzo Stoakes error = mas_store_gfp(&mas_detach, next, GFP_KERNEL); 735*49b1b8d6SLorenzo Stoakes if (error) 736*49b1b8d6SLorenzo Stoakes goto munmap_gather_failed; 737*49b1b8d6SLorenzo Stoakes vma_mark_detached(next, true); 738*49b1b8d6SLorenzo Stoakes if (next->vm_flags & VM_LOCKED) 739*49b1b8d6SLorenzo Stoakes locked_vm += vma_pages(next); 740*49b1b8d6SLorenzo Stoakes 741*49b1b8d6SLorenzo Stoakes count++; 742*49b1b8d6SLorenzo Stoakes if (unlikely(uf)) { 743*49b1b8d6SLorenzo Stoakes /* 744*49b1b8d6SLorenzo Stoakes * If userfaultfd_unmap_prep returns an error the vmas 745*49b1b8d6SLorenzo Stoakes * will remain split, but userland will get a 746*49b1b8d6SLorenzo Stoakes * highly unexpected error anyway. This is no 747*49b1b8d6SLorenzo Stoakes * different than the case where the first of the two 748*49b1b8d6SLorenzo Stoakes * __split_vma fails, but we don't undo the first 749*49b1b8d6SLorenzo Stoakes * split, despite we could. This is unlikely enough 750*49b1b8d6SLorenzo Stoakes * failure that it's not worth optimizing it for. 751*49b1b8d6SLorenzo Stoakes */ 752*49b1b8d6SLorenzo Stoakes error = userfaultfd_unmap_prep(next, start, end, uf); 753*49b1b8d6SLorenzo Stoakes 754*49b1b8d6SLorenzo Stoakes if (error) 755*49b1b8d6SLorenzo Stoakes goto userfaultfd_error; 756*49b1b8d6SLorenzo Stoakes } 757*49b1b8d6SLorenzo Stoakes #ifdef CONFIG_DEBUG_VM_MAPLE_TREE 758*49b1b8d6SLorenzo Stoakes BUG_ON(next->vm_start < start); 759*49b1b8d6SLorenzo Stoakes BUG_ON(next->vm_start > end); 760*49b1b8d6SLorenzo Stoakes #endif 761*49b1b8d6SLorenzo Stoakes } for_each_vma_range(*vmi, next, end); 762*49b1b8d6SLorenzo Stoakes 763*49b1b8d6SLorenzo Stoakes #if defined(CONFIG_DEBUG_VM_MAPLE_TREE) 764*49b1b8d6SLorenzo Stoakes /* Make sure no VMAs are about to be lost. */ 765*49b1b8d6SLorenzo Stoakes { 766*49b1b8d6SLorenzo Stoakes MA_STATE(test, &mt_detach, 0, 0); 767*49b1b8d6SLorenzo Stoakes struct vm_area_struct *vma_mas, *vma_test; 768*49b1b8d6SLorenzo Stoakes int test_count = 0; 769*49b1b8d6SLorenzo Stoakes 770*49b1b8d6SLorenzo Stoakes vma_iter_set(vmi, start); 771*49b1b8d6SLorenzo Stoakes rcu_read_lock(); 772*49b1b8d6SLorenzo Stoakes vma_test = mas_find(&test, count - 1); 773*49b1b8d6SLorenzo Stoakes for_each_vma_range(*vmi, vma_mas, end) { 774*49b1b8d6SLorenzo Stoakes BUG_ON(vma_mas != vma_test); 775*49b1b8d6SLorenzo Stoakes test_count++; 776*49b1b8d6SLorenzo Stoakes vma_test = mas_next(&test, count - 1); 777*49b1b8d6SLorenzo Stoakes } 778*49b1b8d6SLorenzo Stoakes rcu_read_unlock(); 779*49b1b8d6SLorenzo Stoakes BUG_ON(count != test_count); 780*49b1b8d6SLorenzo Stoakes } 781*49b1b8d6SLorenzo Stoakes #endif 782*49b1b8d6SLorenzo Stoakes 783*49b1b8d6SLorenzo Stoakes while (vma_iter_addr(vmi) > start) 784*49b1b8d6SLorenzo Stoakes vma_iter_prev_range(vmi); 785*49b1b8d6SLorenzo Stoakes 786*49b1b8d6SLorenzo Stoakes error = vma_iter_clear_gfp(vmi, start, end, GFP_KERNEL); 787*49b1b8d6SLorenzo Stoakes if (error) 788*49b1b8d6SLorenzo Stoakes goto clear_tree_failed; 789*49b1b8d6SLorenzo Stoakes 790*49b1b8d6SLorenzo Stoakes /* Point of no return */ 791*49b1b8d6SLorenzo Stoakes mm->locked_vm -= locked_vm; 792*49b1b8d6SLorenzo Stoakes mm->map_count -= count; 793*49b1b8d6SLorenzo Stoakes if (unlock) 794*49b1b8d6SLorenzo Stoakes mmap_write_downgrade(mm); 795*49b1b8d6SLorenzo Stoakes 796*49b1b8d6SLorenzo Stoakes prev = vma_iter_prev_range(vmi); 797*49b1b8d6SLorenzo Stoakes next = vma_next(vmi); 798*49b1b8d6SLorenzo Stoakes if (next) 799*49b1b8d6SLorenzo Stoakes vma_iter_prev_range(vmi); 800*49b1b8d6SLorenzo Stoakes 801*49b1b8d6SLorenzo Stoakes /* 802*49b1b8d6SLorenzo Stoakes * We can free page tables without write-locking mmap_lock because VMAs 803*49b1b8d6SLorenzo Stoakes * were isolated before we downgraded mmap_lock. 804*49b1b8d6SLorenzo Stoakes */ 805*49b1b8d6SLorenzo Stoakes mas_set(&mas_detach, 1); 806*49b1b8d6SLorenzo Stoakes unmap_region(mm, &mas_detach, vma, prev, next, start, end, count, 807*49b1b8d6SLorenzo Stoakes !unlock); 808*49b1b8d6SLorenzo Stoakes /* Statistics and freeing VMAs */ 809*49b1b8d6SLorenzo Stoakes mas_set(&mas_detach, 0); 810*49b1b8d6SLorenzo Stoakes remove_mt(mm, &mas_detach); 811*49b1b8d6SLorenzo Stoakes validate_mm(mm); 812*49b1b8d6SLorenzo Stoakes if (unlock) 813*49b1b8d6SLorenzo Stoakes mmap_read_unlock(mm); 814*49b1b8d6SLorenzo Stoakes 815*49b1b8d6SLorenzo Stoakes __mt_destroy(&mt_detach); 816*49b1b8d6SLorenzo Stoakes return 0; 817*49b1b8d6SLorenzo Stoakes 818*49b1b8d6SLorenzo Stoakes clear_tree_failed: 819*49b1b8d6SLorenzo Stoakes userfaultfd_error: 820*49b1b8d6SLorenzo Stoakes munmap_gather_failed: 821*49b1b8d6SLorenzo Stoakes end_split_failed: 822*49b1b8d6SLorenzo Stoakes mas_set(&mas_detach, 0); 823*49b1b8d6SLorenzo Stoakes mas_for_each(&mas_detach, next, end) 824*49b1b8d6SLorenzo Stoakes vma_mark_detached(next, false); 825*49b1b8d6SLorenzo Stoakes 826*49b1b8d6SLorenzo Stoakes __mt_destroy(&mt_detach); 827*49b1b8d6SLorenzo Stoakes start_split_failed: 828*49b1b8d6SLorenzo Stoakes map_count_exceeded: 829*49b1b8d6SLorenzo Stoakes validate_mm(mm); 830*49b1b8d6SLorenzo Stoakes return error; 831*49b1b8d6SLorenzo Stoakes } 832*49b1b8d6SLorenzo Stoakes 833*49b1b8d6SLorenzo Stoakes /* 834*49b1b8d6SLorenzo Stoakes * do_vmi_munmap() - munmap a given range. 835*49b1b8d6SLorenzo Stoakes * @vmi: The vma iterator 836*49b1b8d6SLorenzo Stoakes * @mm: The mm_struct 837*49b1b8d6SLorenzo Stoakes * @start: The start address to munmap 838*49b1b8d6SLorenzo Stoakes * @len: The length of the range to munmap 839*49b1b8d6SLorenzo Stoakes * @uf: The userfaultfd list_head 840*49b1b8d6SLorenzo Stoakes * @unlock: set to true if the user wants to drop the mmap_lock on success 841*49b1b8d6SLorenzo Stoakes * 842*49b1b8d6SLorenzo Stoakes * This function takes a @mas that is either pointing to the previous VMA or set 843*49b1b8d6SLorenzo Stoakes * to MA_START and sets it up to remove the mapping(s). The @len will be 844*49b1b8d6SLorenzo Stoakes * aligned and any arch_unmap work will be preformed. 845*49b1b8d6SLorenzo Stoakes * 846*49b1b8d6SLorenzo Stoakes * Return: 0 on success and drops the lock if so directed, error and leaves the 847*49b1b8d6SLorenzo Stoakes * lock held otherwise. 848*49b1b8d6SLorenzo Stoakes */ 849*49b1b8d6SLorenzo Stoakes int do_vmi_munmap(struct vma_iterator *vmi, struct mm_struct *mm, 850*49b1b8d6SLorenzo Stoakes unsigned long start, size_t len, struct list_head *uf, 851*49b1b8d6SLorenzo Stoakes bool unlock) 852*49b1b8d6SLorenzo Stoakes { 853*49b1b8d6SLorenzo Stoakes unsigned long end; 854*49b1b8d6SLorenzo Stoakes struct vm_area_struct *vma; 855*49b1b8d6SLorenzo Stoakes 856*49b1b8d6SLorenzo Stoakes if ((offset_in_page(start)) || start > TASK_SIZE || len > TASK_SIZE-start) 857*49b1b8d6SLorenzo Stoakes return -EINVAL; 858*49b1b8d6SLorenzo Stoakes 859*49b1b8d6SLorenzo Stoakes end = start + PAGE_ALIGN(len); 860*49b1b8d6SLorenzo Stoakes if (end == start) 861*49b1b8d6SLorenzo Stoakes return -EINVAL; 862*49b1b8d6SLorenzo Stoakes 863*49b1b8d6SLorenzo Stoakes /* 864*49b1b8d6SLorenzo Stoakes * Check if memory is sealed before arch_unmap. 865*49b1b8d6SLorenzo Stoakes * Prevent unmapping a sealed VMA. 866*49b1b8d6SLorenzo Stoakes * can_modify_mm assumes we have acquired the lock on MM. 867*49b1b8d6SLorenzo Stoakes */ 868*49b1b8d6SLorenzo Stoakes if (unlikely(!can_modify_mm(mm, start, end))) 869*49b1b8d6SLorenzo Stoakes return -EPERM; 870*49b1b8d6SLorenzo Stoakes 871*49b1b8d6SLorenzo Stoakes /* arch_unmap() might do unmaps itself. */ 872*49b1b8d6SLorenzo Stoakes arch_unmap(mm, start, end); 873*49b1b8d6SLorenzo Stoakes 874*49b1b8d6SLorenzo Stoakes /* Find the first overlapping VMA */ 875*49b1b8d6SLorenzo Stoakes vma = vma_find(vmi, end); 876*49b1b8d6SLorenzo Stoakes if (!vma) { 877*49b1b8d6SLorenzo Stoakes if (unlock) 878*49b1b8d6SLorenzo Stoakes mmap_write_unlock(mm); 879*49b1b8d6SLorenzo Stoakes return 0; 880*49b1b8d6SLorenzo Stoakes } 881*49b1b8d6SLorenzo Stoakes 882*49b1b8d6SLorenzo Stoakes return do_vmi_align_munmap(vmi, vma, mm, start, end, uf, unlock); 883*49b1b8d6SLorenzo Stoakes } 884*49b1b8d6SLorenzo Stoakes 885*49b1b8d6SLorenzo Stoakes /* 886*49b1b8d6SLorenzo Stoakes * Given a mapping request (addr,end,vm_flags,file,pgoff,anon_name), 887*49b1b8d6SLorenzo Stoakes * figure out whether that can be merged with its predecessor or its 888*49b1b8d6SLorenzo Stoakes * successor. Or both (it neatly fills a hole). 889*49b1b8d6SLorenzo Stoakes * 890*49b1b8d6SLorenzo Stoakes * In most cases - when called for mmap, brk or mremap - [addr,end) is 891*49b1b8d6SLorenzo Stoakes * certain not to be mapped by the time vma_merge is called; but when 892*49b1b8d6SLorenzo Stoakes * called for mprotect, it is certain to be already mapped (either at 893*49b1b8d6SLorenzo Stoakes * an offset within prev, or at the start of next), and the flags of 894*49b1b8d6SLorenzo Stoakes * this area are about to be changed to vm_flags - and the no-change 895*49b1b8d6SLorenzo Stoakes * case has already been eliminated. 896*49b1b8d6SLorenzo Stoakes * 897*49b1b8d6SLorenzo Stoakes * The following mprotect cases have to be considered, where **** is 898*49b1b8d6SLorenzo Stoakes * the area passed down from mprotect_fixup, never extending beyond one 899*49b1b8d6SLorenzo Stoakes * vma, PPPP is the previous vma, CCCC is a concurrent vma that starts 900*49b1b8d6SLorenzo Stoakes * at the same address as **** and is of the same or larger span, and 901*49b1b8d6SLorenzo Stoakes * NNNN the next vma after ****: 902*49b1b8d6SLorenzo Stoakes * 903*49b1b8d6SLorenzo Stoakes * **** **** **** 904*49b1b8d6SLorenzo Stoakes * PPPPPPNNNNNN PPPPPPNNNNNN PPPPPPCCCCCC 905*49b1b8d6SLorenzo Stoakes * cannot merge might become might become 906*49b1b8d6SLorenzo Stoakes * PPNNNNNNNNNN PPPPPPPPPPCC 907*49b1b8d6SLorenzo Stoakes * mmap, brk or case 4 below case 5 below 908*49b1b8d6SLorenzo Stoakes * mremap move: 909*49b1b8d6SLorenzo Stoakes * **** **** 910*49b1b8d6SLorenzo Stoakes * PPPP NNNN PPPPCCCCNNNN 911*49b1b8d6SLorenzo Stoakes * might become might become 912*49b1b8d6SLorenzo Stoakes * PPPPPPPPPPPP 1 or PPPPPPPPPPPP 6 or 913*49b1b8d6SLorenzo Stoakes * PPPPPPPPNNNN 2 or PPPPPPPPNNNN 7 or 914*49b1b8d6SLorenzo Stoakes * PPPPNNNNNNNN 3 PPPPNNNNNNNN 8 915*49b1b8d6SLorenzo Stoakes * 916*49b1b8d6SLorenzo Stoakes * It is important for case 8 that the vma CCCC overlapping the 917*49b1b8d6SLorenzo Stoakes * region **** is never going to extended over NNNN. Instead NNNN must 918*49b1b8d6SLorenzo Stoakes * be extended in region **** and CCCC must be removed. This way in 919*49b1b8d6SLorenzo Stoakes * all cases where vma_merge succeeds, the moment vma_merge drops the 920*49b1b8d6SLorenzo Stoakes * rmap_locks, the properties of the merged vma will be already 921*49b1b8d6SLorenzo Stoakes * correct for the whole merged range. Some of those properties like 922*49b1b8d6SLorenzo Stoakes * vm_page_prot/vm_flags may be accessed by rmap_walks and they must 923*49b1b8d6SLorenzo Stoakes * be correct for the whole merged range immediately after the 924*49b1b8d6SLorenzo Stoakes * rmap_locks are released. Otherwise if NNNN would be removed and 925*49b1b8d6SLorenzo Stoakes * CCCC would be extended over the NNNN range, remove_migration_ptes 926*49b1b8d6SLorenzo Stoakes * or other rmap walkers (if working on addresses beyond the "end" 927*49b1b8d6SLorenzo Stoakes * parameter) may establish ptes with the wrong permissions of CCCC 928*49b1b8d6SLorenzo Stoakes * instead of the right permissions of NNNN. 929*49b1b8d6SLorenzo Stoakes * 930*49b1b8d6SLorenzo Stoakes * In the code below: 931*49b1b8d6SLorenzo Stoakes * PPPP is represented by *prev 932*49b1b8d6SLorenzo Stoakes * CCCC is represented by *curr or not represented at all (NULL) 933*49b1b8d6SLorenzo Stoakes * NNNN is represented by *next or not represented at all (NULL) 934*49b1b8d6SLorenzo Stoakes * **** is not represented - it will be merged and the vma containing the 935*49b1b8d6SLorenzo Stoakes * area is returned, or the function will return NULL 936*49b1b8d6SLorenzo Stoakes */ 937*49b1b8d6SLorenzo Stoakes static struct vm_area_struct 938*49b1b8d6SLorenzo Stoakes *vma_merge(struct vma_iterator *vmi, struct vm_area_struct *prev, 939*49b1b8d6SLorenzo Stoakes struct vm_area_struct *src, unsigned long addr, unsigned long end, 940*49b1b8d6SLorenzo Stoakes unsigned long vm_flags, pgoff_t pgoff, struct mempolicy *policy, 941*49b1b8d6SLorenzo Stoakes struct vm_userfaultfd_ctx vm_userfaultfd_ctx, 942*49b1b8d6SLorenzo Stoakes struct anon_vma_name *anon_name) 943*49b1b8d6SLorenzo Stoakes { 944*49b1b8d6SLorenzo Stoakes struct mm_struct *mm = src->vm_mm; 945*49b1b8d6SLorenzo Stoakes struct anon_vma *anon_vma = src->anon_vma; 946*49b1b8d6SLorenzo Stoakes struct file *file = src->vm_file; 947*49b1b8d6SLorenzo Stoakes struct vm_area_struct *curr, *next, *res; 948*49b1b8d6SLorenzo Stoakes struct vm_area_struct *vma, *adjust, *remove, *remove2; 949*49b1b8d6SLorenzo Stoakes struct vm_area_struct *anon_dup = NULL; 950*49b1b8d6SLorenzo Stoakes struct vma_prepare vp; 951*49b1b8d6SLorenzo Stoakes pgoff_t vma_pgoff; 952*49b1b8d6SLorenzo Stoakes int err = 0; 953*49b1b8d6SLorenzo Stoakes bool merge_prev = false; 954*49b1b8d6SLorenzo Stoakes bool merge_next = false; 955*49b1b8d6SLorenzo Stoakes bool vma_expanded = false; 956*49b1b8d6SLorenzo Stoakes unsigned long vma_start = addr; 957*49b1b8d6SLorenzo Stoakes unsigned long vma_end = end; 958*49b1b8d6SLorenzo Stoakes pgoff_t pglen = (end - addr) >> PAGE_SHIFT; 959*49b1b8d6SLorenzo Stoakes long adj_start = 0; 960*49b1b8d6SLorenzo Stoakes 961*49b1b8d6SLorenzo Stoakes /* 962*49b1b8d6SLorenzo Stoakes * We later require that vma->vm_flags == vm_flags, 963*49b1b8d6SLorenzo Stoakes * so this tests vma->vm_flags & VM_SPECIAL, too. 964*49b1b8d6SLorenzo Stoakes */ 965*49b1b8d6SLorenzo Stoakes if (vm_flags & VM_SPECIAL) 966*49b1b8d6SLorenzo Stoakes return NULL; 967*49b1b8d6SLorenzo Stoakes 968*49b1b8d6SLorenzo Stoakes /* Does the input range span an existing VMA? (cases 5 - 8) */ 969*49b1b8d6SLorenzo Stoakes curr = find_vma_intersection(mm, prev ? prev->vm_end : 0, end); 970*49b1b8d6SLorenzo Stoakes 971*49b1b8d6SLorenzo Stoakes if (!curr || /* cases 1 - 4 */ 972*49b1b8d6SLorenzo Stoakes end == curr->vm_end) /* cases 6 - 8, adjacent VMA */ 973*49b1b8d6SLorenzo Stoakes next = vma_lookup(mm, end); 974*49b1b8d6SLorenzo Stoakes else 975*49b1b8d6SLorenzo Stoakes next = NULL; /* case 5 */ 976*49b1b8d6SLorenzo Stoakes 977*49b1b8d6SLorenzo Stoakes if (prev) { 978*49b1b8d6SLorenzo Stoakes vma_start = prev->vm_start; 979*49b1b8d6SLorenzo Stoakes vma_pgoff = prev->vm_pgoff; 980*49b1b8d6SLorenzo Stoakes 981*49b1b8d6SLorenzo Stoakes /* Can we merge the predecessor? */ 982*49b1b8d6SLorenzo Stoakes if (addr == prev->vm_end && mpol_equal(vma_policy(prev), policy) 983*49b1b8d6SLorenzo Stoakes && can_vma_merge_after(prev, vm_flags, anon_vma, file, 984*49b1b8d6SLorenzo Stoakes pgoff, vm_userfaultfd_ctx, anon_name)) { 985*49b1b8d6SLorenzo Stoakes merge_prev = true; 986*49b1b8d6SLorenzo Stoakes vma_prev(vmi); 987*49b1b8d6SLorenzo Stoakes } 988*49b1b8d6SLorenzo Stoakes } 989*49b1b8d6SLorenzo Stoakes 990*49b1b8d6SLorenzo Stoakes /* Can we merge the successor? */ 991*49b1b8d6SLorenzo Stoakes if (next && mpol_equal(policy, vma_policy(next)) && 992*49b1b8d6SLorenzo Stoakes can_vma_merge_before(next, vm_flags, anon_vma, file, pgoff+pglen, 993*49b1b8d6SLorenzo Stoakes vm_userfaultfd_ctx, anon_name)) { 994*49b1b8d6SLorenzo Stoakes merge_next = true; 995*49b1b8d6SLorenzo Stoakes } 996*49b1b8d6SLorenzo Stoakes 997*49b1b8d6SLorenzo Stoakes /* Verify some invariant that must be enforced by the caller. */ 998*49b1b8d6SLorenzo Stoakes VM_WARN_ON(prev && addr <= prev->vm_start); 999*49b1b8d6SLorenzo Stoakes VM_WARN_ON(curr && (addr != curr->vm_start || end > curr->vm_end)); 1000*49b1b8d6SLorenzo Stoakes VM_WARN_ON(addr >= end); 1001*49b1b8d6SLorenzo Stoakes 1002*49b1b8d6SLorenzo Stoakes if (!merge_prev && !merge_next) 1003*49b1b8d6SLorenzo Stoakes return NULL; /* Not mergeable. */ 1004*49b1b8d6SLorenzo Stoakes 1005*49b1b8d6SLorenzo Stoakes if (merge_prev) 1006*49b1b8d6SLorenzo Stoakes vma_start_write(prev); 1007*49b1b8d6SLorenzo Stoakes 1008*49b1b8d6SLorenzo Stoakes res = vma = prev; 1009*49b1b8d6SLorenzo Stoakes remove = remove2 = adjust = NULL; 1010*49b1b8d6SLorenzo Stoakes 1011*49b1b8d6SLorenzo Stoakes /* Can we merge both the predecessor and the successor? */ 1012*49b1b8d6SLorenzo Stoakes if (merge_prev && merge_next && 1013*49b1b8d6SLorenzo Stoakes is_mergeable_anon_vma(prev->anon_vma, next->anon_vma, NULL)) { 1014*49b1b8d6SLorenzo Stoakes vma_start_write(next); 1015*49b1b8d6SLorenzo Stoakes remove = next; /* case 1 */ 1016*49b1b8d6SLorenzo Stoakes vma_end = next->vm_end; 1017*49b1b8d6SLorenzo Stoakes err = dup_anon_vma(prev, next, &anon_dup); 1018*49b1b8d6SLorenzo Stoakes if (curr) { /* case 6 */ 1019*49b1b8d6SLorenzo Stoakes vma_start_write(curr); 1020*49b1b8d6SLorenzo Stoakes remove = curr; 1021*49b1b8d6SLorenzo Stoakes remove2 = next; 1022*49b1b8d6SLorenzo Stoakes /* 1023*49b1b8d6SLorenzo Stoakes * Note that the dup_anon_vma below cannot overwrite err 1024*49b1b8d6SLorenzo Stoakes * since the first caller would do nothing unless next 1025*49b1b8d6SLorenzo Stoakes * has an anon_vma. 1026*49b1b8d6SLorenzo Stoakes */ 1027*49b1b8d6SLorenzo Stoakes if (!next->anon_vma) 1028*49b1b8d6SLorenzo Stoakes err = dup_anon_vma(prev, curr, &anon_dup); 1029*49b1b8d6SLorenzo Stoakes } 1030*49b1b8d6SLorenzo Stoakes } else if (merge_prev) { /* case 2 */ 1031*49b1b8d6SLorenzo Stoakes if (curr) { 1032*49b1b8d6SLorenzo Stoakes vma_start_write(curr); 1033*49b1b8d6SLorenzo Stoakes if (end == curr->vm_end) { /* case 7 */ 1034*49b1b8d6SLorenzo Stoakes /* 1035*49b1b8d6SLorenzo Stoakes * can_vma_merge_after() assumed we would not be 1036*49b1b8d6SLorenzo Stoakes * removing prev vma, so it skipped the check 1037*49b1b8d6SLorenzo Stoakes * for vm_ops->close, but we are removing curr 1038*49b1b8d6SLorenzo Stoakes */ 1039*49b1b8d6SLorenzo Stoakes if (curr->vm_ops && curr->vm_ops->close) 1040*49b1b8d6SLorenzo Stoakes err = -EINVAL; 1041*49b1b8d6SLorenzo Stoakes remove = curr; 1042*49b1b8d6SLorenzo Stoakes } else { /* case 5 */ 1043*49b1b8d6SLorenzo Stoakes adjust = curr; 1044*49b1b8d6SLorenzo Stoakes adj_start = (end - curr->vm_start); 1045*49b1b8d6SLorenzo Stoakes } 1046*49b1b8d6SLorenzo Stoakes if (!err) 1047*49b1b8d6SLorenzo Stoakes err = dup_anon_vma(prev, curr, &anon_dup); 1048*49b1b8d6SLorenzo Stoakes } 1049*49b1b8d6SLorenzo Stoakes } else { /* merge_next */ 1050*49b1b8d6SLorenzo Stoakes vma_start_write(next); 1051*49b1b8d6SLorenzo Stoakes res = next; 1052*49b1b8d6SLorenzo Stoakes if (prev && addr < prev->vm_end) { /* case 4 */ 1053*49b1b8d6SLorenzo Stoakes vma_start_write(prev); 1054*49b1b8d6SLorenzo Stoakes vma_end = addr; 1055*49b1b8d6SLorenzo Stoakes adjust = next; 1056*49b1b8d6SLorenzo Stoakes adj_start = -(prev->vm_end - addr); 1057*49b1b8d6SLorenzo Stoakes err = dup_anon_vma(next, prev, &anon_dup); 1058*49b1b8d6SLorenzo Stoakes } else { 1059*49b1b8d6SLorenzo Stoakes /* 1060*49b1b8d6SLorenzo Stoakes * Note that cases 3 and 8 are the ONLY ones where prev 1061*49b1b8d6SLorenzo Stoakes * is permitted to be (but is not necessarily) NULL. 1062*49b1b8d6SLorenzo Stoakes */ 1063*49b1b8d6SLorenzo Stoakes vma = next; /* case 3 */ 1064*49b1b8d6SLorenzo Stoakes vma_start = addr; 1065*49b1b8d6SLorenzo Stoakes vma_end = next->vm_end; 1066*49b1b8d6SLorenzo Stoakes vma_pgoff = next->vm_pgoff - pglen; 1067*49b1b8d6SLorenzo Stoakes if (curr) { /* case 8 */ 1068*49b1b8d6SLorenzo Stoakes vma_pgoff = curr->vm_pgoff; 1069*49b1b8d6SLorenzo Stoakes vma_start_write(curr); 1070*49b1b8d6SLorenzo Stoakes remove = curr; 1071*49b1b8d6SLorenzo Stoakes err = dup_anon_vma(next, curr, &anon_dup); 1072*49b1b8d6SLorenzo Stoakes } 1073*49b1b8d6SLorenzo Stoakes } 1074*49b1b8d6SLorenzo Stoakes } 1075*49b1b8d6SLorenzo Stoakes 1076*49b1b8d6SLorenzo Stoakes /* Error in anon_vma clone. */ 1077*49b1b8d6SLorenzo Stoakes if (err) 1078*49b1b8d6SLorenzo Stoakes goto anon_vma_fail; 1079*49b1b8d6SLorenzo Stoakes 1080*49b1b8d6SLorenzo Stoakes if (vma_start < vma->vm_start || vma_end > vma->vm_end) 1081*49b1b8d6SLorenzo Stoakes vma_expanded = true; 1082*49b1b8d6SLorenzo Stoakes 1083*49b1b8d6SLorenzo Stoakes if (vma_expanded) { 1084*49b1b8d6SLorenzo Stoakes vma_iter_config(vmi, vma_start, vma_end); 1085*49b1b8d6SLorenzo Stoakes } else { 1086*49b1b8d6SLorenzo Stoakes vma_iter_config(vmi, adjust->vm_start + adj_start, 1087*49b1b8d6SLorenzo Stoakes adjust->vm_end); 1088*49b1b8d6SLorenzo Stoakes } 1089*49b1b8d6SLorenzo Stoakes 1090*49b1b8d6SLorenzo Stoakes if (vma_iter_prealloc(vmi, vma)) 1091*49b1b8d6SLorenzo Stoakes goto prealloc_fail; 1092*49b1b8d6SLorenzo Stoakes 1093*49b1b8d6SLorenzo Stoakes init_multi_vma_prep(&vp, vma, adjust, remove, remove2); 1094*49b1b8d6SLorenzo Stoakes VM_WARN_ON(vp.anon_vma && adjust && adjust->anon_vma && 1095*49b1b8d6SLorenzo Stoakes vp.anon_vma != adjust->anon_vma); 1096*49b1b8d6SLorenzo Stoakes 1097*49b1b8d6SLorenzo Stoakes vma_prepare(&vp); 1098*49b1b8d6SLorenzo Stoakes vma_adjust_trans_huge(vma, vma_start, vma_end, adj_start); 1099*49b1b8d6SLorenzo Stoakes vma_set_range(vma, vma_start, vma_end, vma_pgoff); 1100*49b1b8d6SLorenzo Stoakes 1101*49b1b8d6SLorenzo Stoakes if (vma_expanded) 1102*49b1b8d6SLorenzo Stoakes vma_iter_store(vmi, vma); 1103*49b1b8d6SLorenzo Stoakes 1104*49b1b8d6SLorenzo Stoakes if (adj_start) { 1105*49b1b8d6SLorenzo Stoakes adjust->vm_start += adj_start; 1106*49b1b8d6SLorenzo Stoakes adjust->vm_pgoff += adj_start >> PAGE_SHIFT; 1107*49b1b8d6SLorenzo Stoakes if (adj_start < 0) { 1108*49b1b8d6SLorenzo Stoakes WARN_ON(vma_expanded); 1109*49b1b8d6SLorenzo Stoakes vma_iter_store(vmi, next); 1110*49b1b8d6SLorenzo Stoakes } 1111*49b1b8d6SLorenzo Stoakes } 1112*49b1b8d6SLorenzo Stoakes 1113*49b1b8d6SLorenzo Stoakes vma_complete(&vp, vmi, mm); 1114*49b1b8d6SLorenzo Stoakes khugepaged_enter_vma(res, vm_flags); 1115*49b1b8d6SLorenzo Stoakes return res; 1116*49b1b8d6SLorenzo Stoakes 1117*49b1b8d6SLorenzo Stoakes prealloc_fail: 1118*49b1b8d6SLorenzo Stoakes if (anon_dup) 1119*49b1b8d6SLorenzo Stoakes unlink_anon_vmas(anon_dup); 1120*49b1b8d6SLorenzo Stoakes 1121*49b1b8d6SLorenzo Stoakes anon_vma_fail: 1122*49b1b8d6SLorenzo Stoakes vma_iter_set(vmi, addr); 1123*49b1b8d6SLorenzo Stoakes vma_iter_load(vmi); 1124*49b1b8d6SLorenzo Stoakes return NULL; 1125*49b1b8d6SLorenzo Stoakes } 1126*49b1b8d6SLorenzo Stoakes 1127*49b1b8d6SLorenzo Stoakes /* 1128*49b1b8d6SLorenzo Stoakes * We are about to modify one or multiple of a VMA's flags, policy, userfaultfd 1129*49b1b8d6SLorenzo Stoakes * context and anonymous VMA name within the range [start, end). 1130*49b1b8d6SLorenzo Stoakes * 1131*49b1b8d6SLorenzo Stoakes * As a result, we might be able to merge the newly modified VMA range with an 1132*49b1b8d6SLorenzo Stoakes * adjacent VMA with identical properties. 1133*49b1b8d6SLorenzo Stoakes * 1134*49b1b8d6SLorenzo Stoakes * If no merge is possible and the range does not span the entirety of the VMA, 1135*49b1b8d6SLorenzo Stoakes * we then need to split the VMA to accommodate the change. 1136*49b1b8d6SLorenzo Stoakes * 1137*49b1b8d6SLorenzo Stoakes * The function returns either the merged VMA, the original VMA if a split was 1138*49b1b8d6SLorenzo Stoakes * required instead, or an error if the split failed. 1139*49b1b8d6SLorenzo Stoakes */ 1140*49b1b8d6SLorenzo Stoakes struct vm_area_struct *vma_modify(struct vma_iterator *vmi, 1141*49b1b8d6SLorenzo Stoakes struct vm_area_struct *prev, 1142*49b1b8d6SLorenzo Stoakes struct vm_area_struct *vma, 1143*49b1b8d6SLorenzo Stoakes unsigned long start, unsigned long end, 1144*49b1b8d6SLorenzo Stoakes unsigned long vm_flags, 1145*49b1b8d6SLorenzo Stoakes struct mempolicy *policy, 1146*49b1b8d6SLorenzo Stoakes struct vm_userfaultfd_ctx uffd_ctx, 1147*49b1b8d6SLorenzo Stoakes struct anon_vma_name *anon_name) 1148*49b1b8d6SLorenzo Stoakes { 1149*49b1b8d6SLorenzo Stoakes pgoff_t pgoff = vma->vm_pgoff + ((start - vma->vm_start) >> PAGE_SHIFT); 1150*49b1b8d6SLorenzo Stoakes struct vm_area_struct *merged; 1151*49b1b8d6SLorenzo Stoakes 1152*49b1b8d6SLorenzo Stoakes merged = vma_merge(vmi, prev, vma, start, end, vm_flags, 1153*49b1b8d6SLorenzo Stoakes pgoff, policy, uffd_ctx, anon_name); 1154*49b1b8d6SLorenzo Stoakes if (merged) 1155*49b1b8d6SLorenzo Stoakes return merged; 1156*49b1b8d6SLorenzo Stoakes 1157*49b1b8d6SLorenzo Stoakes if (vma->vm_start < start) { 1158*49b1b8d6SLorenzo Stoakes int err = split_vma(vmi, vma, start, 1); 1159*49b1b8d6SLorenzo Stoakes 1160*49b1b8d6SLorenzo Stoakes if (err) 1161*49b1b8d6SLorenzo Stoakes return ERR_PTR(err); 1162*49b1b8d6SLorenzo Stoakes } 1163*49b1b8d6SLorenzo Stoakes 1164*49b1b8d6SLorenzo Stoakes if (vma->vm_end > end) { 1165*49b1b8d6SLorenzo Stoakes int err = split_vma(vmi, vma, end, 0); 1166*49b1b8d6SLorenzo Stoakes 1167*49b1b8d6SLorenzo Stoakes if (err) 1168*49b1b8d6SLorenzo Stoakes return ERR_PTR(err); 1169*49b1b8d6SLorenzo Stoakes } 1170*49b1b8d6SLorenzo Stoakes 1171*49b1b8d6SLorenzo Stoakes return vma; 1172*49b1b8d6SLorenzo Stoakes } 1173*49b1b8d6SLorenzo Stoakes 1174*49b1b8d6SLorenzo Stoakes /* 1175*49b1b8d6SLorenzo Stoakes * Attempt to merge a newly mapped VMA with those adjacent to it. The caller 1176*49b1b8d6SLorenzo Stoakes * must ensure that [start, end) does not overlap any existing VMA. 1177*49b1b8d6SLorenzo Stoakes */ 1178*49b1b8d6SLorenzo Stoakes struct vm_area_struct 1179*49b1b8d6SLorenzo Stoakes *vma_merge_new_vma(struct vma_iterator *vmi, struct vm_area_struct *prev, 1180*49b1b8d6SLorenzo Stoakes struct vm_area_struct *vma, unsigned long start, 1181*49b1b8d6SLorenzo Stoakes unsigned long end, pgoff_t pgoff) 1182*49b1b8d6SLorenzo Stoakes { 1183*49b1b8d6SLorenzo Stoakes return vma_merge(vmi, prev, vma, start, end, vma->vm_flags, pgoff, 1184*49b1b8d6SLorenzo Stoakes vma_policy(vma), vma->vm_userfaultfd_ctx, anon_vma_name(vma)); 1185*49b1b8d6SLorenzo Stoakes } 1186*49b1b8d6SLorenzo Stoakes 1187*49b1b8d6SLorenzo Stoakes /* 1188*49b1b8d6SLorenzo Stoakes * Expand vma by delta bytes, potentially merging with an immediately adjacent 1189*49b1b8d6SLorenzo Stoakes * VMA with identical properties. 1190*49b1b8d6SLorenzo Stoakes */ 1191*49b1b8d6SLorenzo Stoakes struct vm_area_struct *vma_merge_extend(struct vma_iterator *vmi, 1192*49b1b8d6SLorenzo Stoakes struct vm_area_struct *vma, 1193*49b1b8d6SLorenzo Stoakes unsigned long delta) 1194*49b1b8d6SLorenzo Stoakes { 1195*49b1b8d6SLorenzo Stoakes pgoff_t pgoff = vma->vm_pgoff + vma_pages(vma); 1196*49b1b8d6SLorenzo Stoakes 1197*49b1b8d6SLorenzo Stoakes /* vma is specified as prev, so case 1 or 2 will apply. */ 1198*49b1b8d6SLorenzo Stoakes return vma_merge(vmi, vma, vma, vma->vm_end, vma->vm_end + delta, 1199*49b1b8d6SLorenzo Stoakes vma->vm_flags, pgoff, vma_policy(vma), 1200*49b1b8d6SLorenzo Stoakes vma->vm_userfaultfd_ctx, anon_vma_name(vma)); 1201*49b1b8d6SLorenzo Stoakes } 1202*49b1b8d6SLorenzo Stoakes 1203*49b1b8d6SLorenzo Stoakes void unlink_file_vma_batch_init(struct unlink_vma_file_batch *vb) 1204*49b1b8d6SLorenzo Stoakes { 1205*49b1b8d6SLorenzo Stoakes vb->count = 0; 1206*49b1b8d6SLorenzo Stoakes } 1207*49b1b8d6SLorenzo Stoakes 1208*49b1b8d6SLorenzo Stoakes static void unlink_file_vma_batch_process(struct unlink_vma_file_batch *vb) 1209*49b1b8d6SLorenzo Stoakes { 1210*49b1b8d6SLorenzo Stoakes struct address_space *mapping; 1211*49b1b8d6SLorenzo Stoakes int i; 1212*49b1b8d6SLorenzo Stoakes 1213*49b1b8d6SLorenzo Stoakes mapping = vb->vmas[0]->vm_file->f_mapping; 1214*49b1b8d6SLorenzo Stoakes i_mmap_lock_write(mapping); 1215*49b1b8d6SLorenzo Stoakes for (i = 0; i < vb->count; i++) { 1216*49b1b8d6SLorenzo Stoakes VM_WARN_ON_ONCE(vb->vmas[i]->vm_file->f_mapping != mapping); 1217*49b1b8d6SLorenzo Stoakes __remove_shared_vm_struct(vb->vmas[i], mapping); 1218*49b1b8d6SLorenzo Stoakes } 1219*49b1b8d6SLorenzo Stoakes i_mmap_unlock_write(mapping); 1220*49b1b8d6SLorenzo Stoakes 1221*49b1b8d6SLorenzo Stoakes unlink_file_vma_batch_init(vb); 1222*49b1b8d6SLorenzo Stoakes } 1223*49b1b8d6SLorenzo Stoakes 1224*49b1b8d6SLorenzo Stoakes void unlink_file_vma_batch_add(struct unlink_vma_file_batch *vb, 1225*49b1b8d6SLorenzo Stoakes struct vm_area_struct *vma) 1226*49b1b8d6SLorenzo Stoakes { 1227*49b1b8d6SLorenzo Stoakes if (vma->vm_file == NULL) 1228*49b1b8d6SLorenzo Stoakes return; 1229*49b1b8d6SLorenzo Stoakes 1230*49b1b8d6SLorenzo Stoakes if ((vb->count > 0 && vb->vmas[0]->vm_file != vma->vm_file) || 1231*49b1b8d6SLorenzo Stoakes vb->count == ARRAY_SIZE(vb->vmas)) 1232*49b1b8d6SLorenzo Stoakes unlink_file_vma_batch_process(vb); 1233*49b1b8d6SLorenzo Stoakes 1234*49b1b8d6SLorenzo Stoakes vb->vmas[vb->count] = vma; 1235*49b1b8d6SLorenzo Stoakes vb->count++; 1236*49b1b8d6SLorenzo Stoakes } 1237*49b1b8d6SLorenzo Stoakes 1238*49b1b8d6SLorenzo Stoakes void unlink_file_vma_batch_final(struct unlink_vma_file_batch *vb) 1239*49b1b8d6SLorenzo Stoakes { 1240*49b1b8d6SLorenzo Stoakes if (vb->count > 0) 1241*49b1b8d6SLorenzo Stoakes unlink_file_vma_batch_process(vb); 1242*49b1b8d6SLorenzo Stoakes } 1243*49b1b8d6SLorenzo Stoakes 1244*49b1b8d6SLorenzo Stoakes /* 1245*49b1b8d6SLorenzo Stoakes * Unlink a file-based vm structure from its interval tree, to hide 1246*49b1b8d6SLorenzo Stoakes * vma from rmap and vmtruncate before freeing its page tables. 1247*49b1b8d6SLorenzo Stoakes */ 1248*49b1b8d6SLorenzo Stoakes void unlink_file_vma(struct vm_area_struct *vma) 1249*49b1b8d6SLorenzo Stoakes { 1250*49b1b8d6SLorenzo Stoakes struct file *file = vma->vm_file; 1251*49b1b8d6SLorenzo Stoakes 1252*49b1b8d6SLorenzo Stoakes if (file) { 1253*49b1b8d6SLorenzo Stoakes struct address_space *mapping = file->f_mapping; 1254*49b1b8d6SLorenzo Stoakes 1255*49b1b8d6SLorenzo Stoakes i_mmap_lock_write(mapping); 1256*49b1b8d6SLorenzo Stoakes __remove_shared_vm_struct(vma, mapping); 1257*49b1b8d6SLorenzo Stoakes i_mmap_unlock_write(mapping); 1258*49b1b8d6SLorenzo Stoakes } 1259*49b1b8d6SLorenzo Stoakes } 1260*49b1b8d6SLorenzo Stoakes 1261*49b1b8d6SLorenzo Stoakes void vma_link_file(struct vm_area_struct *vma) 1262*49b1b8d6SLorenzo Stoakes { 1263*49b1b8d6SLorenzo Stoakes struct file *file = vma->vm_file; 1264*49b1b8d6SLorenzo Stoakes struct address_space *mapping; 1265*49b1b8d6SLorenzo Stoakes 1266*49b1b8d6SLorenzo Stoakes if (file) { 1267*49b1b8d6SLorenzo Stoakes mapping = file->f_mapping; 1268*49b1b8d6SLorenzo Stoakes i_mmap_lock_write(mapping); 1269*49b1b8d6SLorenzo Stoakes __vma_link_file(vma, mapping); 1270*49b1b8d6SLorenzo Stoakes i_mmap_unlock_write(mapping); 1271*49b1b8d6SLorenzo Stoakes } 1272*49b1b8d6SLorenzo Stoakes } 1273*49b1b8d6SLorenzo Stoakes 1274*49b1b8d6SLorenzo Stoakes int vma_link(struct mm_struct *mm, struct vm_area_struct *vma) 1275*49b1b8d6SLorenzo Stoakes { 1276*49b1b8d6SLorenzo Stoakes VMA_ITERATOR(vmi, mm, 0); 1277*49b1b8d6SLorenzo Stoakes 1278*49b1b8d6SLorenzo Stoakes vma_iter_config(&vmi, vma->vm_start, vma->vm_end); 1279*49b1b8d6SLorenzo Stoakes if (vma_iter_prealloc(&vmi, vma)) 1280*49b1b8d6SLorenzo Stoakes return -ENOMEM; 1281*49b1b8d6SLorenzo Stoakes 1282*49b1b8d6SLorenzo Stoakes vma_start_write(vma); 1283*49b1b8d6SLorenzo Stoakes vma_iter_store(&vmi, vma); 1284*49b1b8d6SLorenzo Stoakes vma_link_file(vma); 1285*49b1b8d6SLorenzo Stoakes mm->map_count++; 1286*49b1b8d6SLorenzo Stoakes validate_mm(mm); 1287*49b1b8d6SLorenzo Stoakes return 0; 1288*49b1b8d6SLorenzo Stoakes } 1289*49b1b8d6SLorenzo Stoakes 1290*49b1b8d6SLorenzo Stoakes /* 1291*49b1b8d6SLorenzo Stoakes * Copy the vma structure to a new location in the same mm, 1292*49b1b8d6SLorenzo Stoakes * prior to moving page table entries, to effect an mremap move. 1293*49b1b8d6SLorenzo Stoakes */ 1294*49b1b8d6SLorenzo Stoakes struct vm_area_struct *copy_vma(struct vm_area_struct **vmap, 1295*49b1b8d6SLorenzo Stoakes unsigned long addr, unsigned long len, pgoff_t pgoff, 1296*49b1b8d6SLorenzo Stoakes bool *need_rmap_locks) 1297*49b1b8d6SLorenzo Stoakes { 1298*49b1b8d6SLorenzo Stoakes struct vm_area_struct *vma = *vmap; 1299*49b1b8d6SLorenzo Stoakes unsigned long vma_start = vma->vm_start; 1300*49b1b8d6SLorenzo Stoakes struct mm_struct *mm = vma->vm_mm; 1301*49b1b8d6SLorenzo Stoakes struct vm_area_struct *new_vma, *prev; 1302*49b1b8d6SLorenzo Stoakes bool faulted_in_anon_vma = true; 1303*49b1b8d6SLorenzo Stoakes VMA_ITERATOR(vmi, mm, addr); 1304*49b1b8d6SLorenzo Stoakes 1305*49b1b8d6SLorenzo Stoakes /* 1306*49b1b8d6SLorenzo Stoakes * If anonymous vma has not yet been faulted, update new pgoff 1307*49b1b8d6SLorenzo Stoakes * to match new location, to increase its chance of merging. 1308*49b1b8d6SLorenzo Stoakes */ 1309*49b1b8d6SLorenzo Stoakes if (unlikely(vma_is_anonymous(vma) && !vma->anon_vma)) { 1310*49b1b8d6SLorenzo Stoakes pgoff = addr >> PAGE_SHIFT; 1311*49b1b8d6SLorenzo Stoakes faulted_in_anon_vma = false; 1312*49b1b8d6SLorenzo Stoakes } 1313*49b1b8d6SLorenzo Stoakes 1314*49b1b8d6SLorenzo Stoakes new_vma = find_vma_prev(mm, addr, &prev); 1315*49b1b8d6SLorenzo Stoakes if (new_vma && new_vma->vm_start < addr + len) 1316*49b1b8d6SLorenzo Stoakes return NULL; /* should never get here */ 1317*49b1b8d6SLorenzo Stoakes 1318*49b1b8d6SLorenzo Stoakes new_vma = vma_merge_new_vma(&vmi, prev, vma, addr, addr + len, pgoff); 1319*49b1b8d6SLorenzo Stoakes if (new_vma) { 1320*49b1b8d6SLorenzo Stoakes /* 1321*49b1b8d6SLorenzo Stoakes * Source vma may have been merged into new_vma 1322*49b1b8d6SLorenzo Stoakes */ 1323*49b1b8d6SLorenzo Stoakes if (unlikely(vma_start >= new_vma->vm_start && 1324*49b1b8d6SLorenzo Stoakes vma_start < new_vma->vm_end)) { 1325*49b1b8d6SLorenzo Stoakes /* 1326*49b1b8d6SLorenzo Stoakes * The only way we can get a vma_merge with 1327*49b1b8d6SLorenzo Stoakes * self during an mremap is if the vma hasn't 1328*49b1b8d6SLorenzo Stoakes * been faulted in yet and we were allowed to 1329*49b1b8d6SLorenzo Stoakes * reset the dst vma->vm_pgoff to the 1330*49b1b8d6SLorenzo Stoakes * destination address of the mremap to allow 1331*49b1b8d6SLorenzo Stoakes * the merge to happen. mremap must change the 1332*49b1b8d6SLorenzo Stoakes * vm_pgoff linearity between src and dst vmas 1333*49b1b8d6SLorenzo Stoakes * (in turn preventing a vma_merge) to be 1334*49b1b8d6SLorenzo Stoakes * safe. It is only safe to keep the vm_pgoff 1335*49b1b8d6SLorenzo Stoakes * linear if there are no pages mapped yet. 1336*49b1b8d6SLorenzo Stoakes */ 1337*49b1b8d6SLorenzo Stoakes VM_BUG_ON_VMA(faulted_in_anon_vma, new_vma); 1338*49b1b8d6SLorenzo Stoakes *vmap = vma = new_vma; 1339*49b1b8d6SLorenzo Stoakes } 1340*49b1b8d6SLorenzo Stoakes *need_rmap_locks = (new_vma->vm_pgoff <= vma->vm_pgoff); 1341*49b1b8d6SLorenzo Stoakes } else { 1342*49b1b8d6SLorenzo Stoakes new_vma = vm_area_dup(vma); 1343*49b1b8d6SLorenzo Stoakes if (!new_vma) 1344*49b1b8d6SLorenzo Stoakes goto out; 1345*49b1b8d6SLorenzo Stoakes vma_set_range(new_vma, addr, addr + len, pgoff); 1346*49b1b8d6SLorenzo Stoakes if (vma_dup_policy(vma, new_vma)) 1347*49b1b8d6SLorenzo Stoakes goto out_free_vma; 1348*49b1b8d6SLorenzo Stoakes if (anon_vma_clone(new_vma, vma)) 1349*49b1b8d6SLorenzo Stoakes goto out_free_mempol; 1350*49b1b8d6SLorenzo Stoakes if (new_vma->vm_file) 1351*49b1b8d6SLorenzo Stoakes get_file(new_vma->vm_file); 1352*49b1b8d6SLorenzo Stoakes if (new_vma->vm_ops && new_vma->vm_ops->open) 1353*49b1b8d6SLorenzo Stoakes new_vma->vm_ops->open(new_vma); 1354*49b1b8d6SLorenzo Stoakes if (vma_link(mm, new_vma)) 1355*49b1b8d6SLorenzo Stoakes goto out_vma_link; 1356*49b1b8d6SLorenzo Stoakes *need_rmap_locks = false; 1357*49b1b8d6SLorenzo Stoakes } 1358*49b1b8d6SLorenzo Stoakes return new_vma; 1359*49b1b8d6SLorenzo Stoakes 1360*49b1b8d6SLorenzo Stoakes out_vma_link: 1361*49b1b8d6SLorenzo Stoakes if (new_vma->vm_ops && new_vma->vm_ops->close) 1362*49b1b8d6SLorenzo Stoakes new_vma->vm_ops->close(new_vma); 1363*49b1b8d6SLorenzo Stoakes 1364*49b1b8d6SLorenzo Stoakes if (new_vma->vm_file) 1365*49b1b8d6SLorenzo Stoakes fput(new_vma->vm_file); 1366*49b1b8d6SLorenzo Stoakes 1367*49b1b8d6SLorenzo Stoakes unlink_anon_vmas(new_vma); 1368*49b1b8d6SLorenzo Stoakes out_free_mempol: 1369*49b1b8d6SLorenzo Stoakes mpol_put(vma_policy(new_vma)); 1370*49b1b8d6SLorenzo Stoakes out_free_vma: 1371*49b1b8d6SLorenzo Stoakes vm_area_free(new_vma); 1372*49b1b8d6SLorenzo Stoakes out: 1373*49b1b8d6SLorenzo Stoakes return NULL; 1374*49b1b8d6SLorenzo Stoakes } 1375*49b1b8d6SLorenzo Stoakes 1376*49b1b8d6SLorenzo Stoakes /* 1377*49b1b8d6SLorenzo Stoakes * Rough compatibility check to quickly see if it's even worth looking 1378*49b1b8d6SLorenzo Stoakes * at sharing an anon_vma. 1379*49b1b8d6SLorenzo Stoakes * 1380*49b1b8d6SLorenzo Stoakes * They need to have the same vm_file, and the flags can only differ 1381*49b1b8d6SLorenzo Stoakes * in things that mprotect may change. 1382*49b1b8d6SLorenzo Stoakes * 1383*49b1b8d6SLorenzo Stoakes * NOTE! The fact that we share an anon_vma doesn't _have_ to mean that 1384*49b1b8d6SLorenzo Stoakes * we can merge the two vma's. For example, we refuse to merge a vma if 1385*49b1b8d6SLorenzo Stoakes * there is a vm_ops->close() function, because that indicates that the 1386*49b1b8d6SLorenzo Stoakes * driver is doing some kind of reference counting. But that doesn't 1387*49b1b8d6SLorenzo Stoakes * really matter for the anon_vma sharing case. 1388*49b1b8d6SLorenzo Stoakes */ 1389*49b1b8d6SLorenzo Stoakes static int anon_vma_compatible(struct vm_area_struct *a, struct vm_area_struct *b) 1390*49b1b8d6SLorenzo Stoakes { 1391*49b1b8d6SLorenzo Stoakes return a->vm_end == b->vm_start && 1392*49b1b8d6SLorenzo Stoakes mpol_equal(vma_policy(a), vma_policy(b)) && 1393*49b1b8d6SLorenzo Stoakes a->vm_file == b->vm_file && 1394*49b1b8d6SLorenzo Stoakes !((a->vm_flags ^ b->vm_flags) & ~(VM_ACCESS_FLAGS | VM_SOFTDIRTY)) && 1395*49b1b8d6SLorenzo Stoakes b->vm_pgoff == a->vm_pgoff + ((b->vm_start - a->vm_start) >> PAGE_SHIFT); 1396*49b1b8d6SLorenzo Stoakes } 1397*49b1b8d6SLorenzo Stoakes 1398*49b1b8d6SLorenzo Stoakes /* 1399*49b1b8d6SLorenzo Stoakes * Do some basic sanity checking to see if we can re-use the anon_vma 1400*49b1b8d6SLorenzo Stoakes * from 'old'. The 'a'/'b' vma's are in VM order - one of them will be 1401*49b1b8d6SLorenzo Stoakes * the same as 'old', the other will be the new one that is trying 1402*49b1b8d6SLorenzo Stoakes * to share the anon_vma. 1403*49b1b8d6SLorenzo Stoakes * 1404*49b1b8d6SLorenzo Stoakes * NOTE! This runs with mmap_lock held for reading, so it is possible that 1405*49b1b8d6SLorenzo Stoakes * the anon_vma of 'old' is concurrently in the process of being set up 1406*49b1b8d6SLorenzo Stoakes * by another page fault trying to merge _that_. But that's ok: if it 1407*49b1b8d6SLorenzo Stoakes * is being set up, that automatically means that it will be a singleton 1408*49b1b8d6SLorenzo Stoakes * acceptable for merging, so we can do all of this optimistically. But 1409*49b1b8d6SLorenzo Stoakes * we do that READ_ONCE() to make sure that we never re-load the pointer. 1410*49b1b8d6SLorenzo Stoakes * 1411*49b1b8d6SLorenzo Stoakes * IOW: that the "list_is_singular()" test on the anon_vma_chain only 1412*49b1b8d6SLorenzo Stoakes * matters for the 'stable anon_vma' case (ie the thing we want to avoid 1413*49b1b8d6SLorenzo Stoakes * is to return an anon_vma that is "complex" due to having gone through 1414*49b1b8d6SLorenzo Stoakes * a fork). 1415*49b1b8d6SLorenzo Stoakes * 1416*49b1b8d6SLorenzo Stoakes * We also make sure that the two vma's are compatible (adjacent, 1417*49b1b8d6SLorenzo Stoakes * and with the same memory policies). That's all stable, even with just 1418*49b1b8d6SLorenzo Stoakes * a read lock on the mmap_lock. 1419*49b1b8d6SLorenzo Stoakes */ 1420*49b1b8d6SLorenzo Stoakes static struct anon_vma *reusable_anon_vma(struct vm_area_struct *old, 1421*49b1b8d6SLorenzo Stoakes struct vm_area_struct *a, 1422*49b1b8d6SLorenzo Stoakes struct vm_area_struct *b) 1423*49b1b8d6SLorenzo Stoakes { 1424*49b1b8d6SLorenzo Stoakes if (anon_vma_compatible(a, b)) { 1425*49b1b8d6SLorenzo Stoakes struct anon_vma *anon_vma = READ_ONCE(old->anon_vma); 1426*49b1b8d6SLorenzo Stoakes 1427*49b1b8d6SLorenzo Stoakes if (anon_vma && list_is_singular(&old->anon_vma_chain)) 1428*49b1b8d6SLorenzo Stoakes return anon_vma; 1429*49b1b8d6SLorenzo Stoakes } 1430*49b1b8d6SLorenzo Stoakes return NULL; 1431*49b1b8d6SLorenzo Stoakes } 1432*49b1b8d6SLorenzo Stoakes 1433*49b1b8d6SLorenzo Stoakes /* 1434*49b1b8d6SLorenzo Stoakes * find_mergeable_anon_vma is used by anon_vma_prepare, to check 1435*49b1b8d6SLorenzo Stoakes * neighbouring vmas for a suitable anon_vma, before it goes off 1436*49b1b8d6SLorenzo Stoakes * to allocate a new anon_vma. It checks because a repetitive 1437*49b1b8d6SLorenzo Stoakes * sequence of mprotects and faults may otherwise lead to distinct 1438*49b1b8d6SLorenzo Stoakes * anon_vmas being allocated, preventing vma merge in subsequent 1439*49b1b8d6SLorenzo Stoakes * mprotect. 1440*49b1b8d6SLorenzo Stoakes */ 1441*49b1b8d6SLorenzo Stoakes struct anon_vma *find_mergeable_anon_vma(struct vm_area_struct *vma) 1442*49b1b8d6SLorenzo Stoakes { 1443*49b1b8d6SLorenzo Stoakes struct anon_vma *anon_vma = NULL; 1444*49b1b8d6SLorenzo Stoakes struct vm_area_struct *prev, *next; 1445*49b1b8d6SLorenzo Stoakes VMA_ITERATOR(vmi, vma->vm_mm, vma->vm_end); 1446*49b1b8d6SLorenzo Stoakes 1447*49b1b8d6SLorenzo Stoakes /* Try next first. */ 1448*49b1b8d6SLorenzo Stoakes next = vma_iter_load(&vmi); 1449*49b1b8d6SLorenzo Stoakes if (next) { 1450*49b1b8d6SLorenzo Stoakes anon_vma = reusable_anon_vma(next, vma, next); 1451*49b1b8d6SLorenzo Stoakes if (anon_vma) 1452*49b1b8d6SLorenzo Stoakes return anon_vma; 1453*49b1b8d6SLorenzo Stoakes } 1454*49b1b8d6SLorenzo Stoakes 1455*49b1b8d6SLorenzo Stoakes prev = vma_prev(&vmi); 1456*49b1b8d6SLorenzo Stoakes VM_BUG_ON_VMA(prev != vma, vma); 1457*49b1b8d6SLorenzo Stoakes prev = vma_prev(&vmi); 1458*49b1b8d6SLorenzo Stoakes /* Try prev next. */ 1459*49b1b8d6SLorenzo Stoakes if (prev) 1460*49b1b8d6SLorenzo Stoakes anon_vma = reusable_anon_vma(prev, prev, vma); 1461*49b1b8d6SLorenzo Stoakes 1462*49b1b8d6SLorenzo Stoakes /* 1463*49b1b8d6SLorenzo Stoakes * We might reach here with anon_vma == NULL if we can't find 1464*49b1b8d6SLorenzo Stoakes * any reusable anon_vma. 1465*49b1b8d6SLorenzo Stoakes * There's no absolute need to look only at touching neighbours: 1466*49b1b8d6SLorenzo Stoakes * we could search further afield for "compatible" anon_vmas. 1467*49b1b8d6SLorenzo Stoakes * But it would probably just be a waste of time searching, 1468*49b1b8d6SLorenzo Stoakes * or lead to too many vmas hanging off the same anon_vma. 1469*49b1b8d6SLorenzo Stoakes * We're trying to allow mprotect remerging later on, 1470*49b1b8d6SLorenzo Stoakes * not trying to minimize memory used for anon_vmas. 1471*49b1b8d6SLorenzo Stoakes */ 1472*49b1b8d6SLorenzo Stoakes return anon_vma; 1473*49b1b8d6SLorenzo Stoakes } 1474*49b1b8d6SLorenzo Stoakes 1475*49b1b8d6SLorenzo Stoakes static bool vm_ops_needs_writenotify(const struct vm_operations_struct *vm_ops) 1476*49b1b8d6SLorenzo Stoakes { 1477*49b1b8d6SLorenzo Stoakes return vm_ops && (vm_ops->page_mkwrite || vm_ops->pfn_mkwrite); 1478*49b1b8d6SLorenzo Stoakes } 1479*49b1b8d6SLorenzo Stoakes 1480*49b1b8d6SLorenzo Stoakes static bool vma_is_shared_writable(struct vm_area_struct *vma) 1481*49b1b8d6SLorenzo Stoakes { 1482*49b1b8d6SLorenzo Stoakes return (vma->vm_flags & (VM_WRITE | VM_SHARED)) == 1483*49b1b8d6SLorenzo Stoakes (VM_WRITE | VM_SHARED); 1484*49b1b8d6SLorenzo Stoakes } 1485*49b1b8d6SLorenzo Stoakes 1486*49b1b8d6SLorenzo Stoakes static bool vma_fs_can_writeback(struct vm_area_struct *vma) 1487*49b1b8d6SLorenzo Stoakes { 1488*49b1b8d6SLorenzo Stoakes /* No managed pages to writeback. */ 1489*49b1b8d6SLorenzo Stoakes if (vma->vm_flags & VM_PFNMAP) 1490*49b1b8d6SLorenzo Stoakes return false; 1491*49b1b8d6SLorenzo Stoakes 1492*49b1b8d6SLorenzo Stoakes return vma->vm_file && vma->vm_file->f_mapping && 1493*49b1b8d6SLorenzo Stoakes mapping_can_writeback(vma->vm_file->f_mapping); 1494*49b1b8d6SLorenzo Stoakes } 1495*49b1b8d6SLorenzo Stoakes 1496*49b1b8d6SLorenzo Stoakes /* 1497*49b1b8d6SLorenzo Stoakes * Does this VMA require the underlying folios to have their dirty state 1498*49b1b8d6SLorenzo Stoakes * tracked? 1499*49b1b8d6SLorenzo Stoakes */ 1500*49b1b8d6SLorenzo Stoakes bool vma_needs_dirty_tracking(struct vm_area_struct *vma) 1501*49b1b8d6SLorenzo Stoakes { 1502*49b1b8d6SLorenzo Stoakes /* Only shared, writable VMAs require dirty tracking. */ 1503*49b1b8d6SLorenzo Stoakes if (!vma_is_shared_writable(vma)) 1504*49b1b8d6SLorenzo Stoakes return false; 1505*49b1b8d6SLorenzo Stoakes 1506*49b1b8d6SLorenzo Stoakes /* Does the filesystem need to be notified? */ 1507*49b1b8d6SLorenzo Stoakes if (vm_ops_needs_writenotify(vma->vm_ops)) 1508*49b1b8d6SLorenzo Stoakes return true; 1509*49b1b8d6SLorenzo Stoakes 1510*49b1b8d6SLorenzo Stoakes /* 1511*49b1b8d6SLorenzo Stoakes * Even if the filesystem doesn't indicate a need for writenotify, if it 1512*49b1b8d6SLorenzo Stoakes * can writeback, dirty tracking is still required. 1513*49b1b8d6SLorenzo Stoakes */ 1514*49b1b8d6SLorenzo Stoakes return vma_fs_can_writeback(vma); 1515*49b1b8d6SLorenzo Stoakes } 1516*49b1b8d6SLorenzo Stoakes 1517*49b1b8d6SLorenzo Stoakes /* 1518*49b1b8d6SLorenzo Stoakes * Some shared mappings will want the pages marked read-only 1519*49b1b8d6SLorenzo Stoakes * to track write events. If so, we'll downgrade vm_page_prot 1520*49b1b8d6SLorenzo Stoakes * to the private version (using protection_map[] without the 1521*49b1b8d6SLorenzo Stoakes * VM_SHARED bit). 1522*49b1b8d6SLorenzo Stoakes */ 1523*49b1b8d6SLorenzo Stoakes bool vma_wants_writenotify(struct vm_area_struct *vma, pgprot_t vm_page_prot) 1524*49b1b8d6SLorenzo Stoakes { 1525*49b1b8d6SLorenzo Stoakes /* If it was private or non-writable, the write bit is already clear */ 1526*49b1b8d6SLorenzo Stoakes if (!vma_is_shared_writable(vma)) 1527*49b1b8d6SLorenzo Stoakes return false; 1528*49b1b8d6SLorenzo Stoakes 1529*49b1b8d6SLorenzo Stoakes /* The backer wishes to know when pages are first written to? */ 1530*49b1b8d6SLorenzo Stoakes if (vm_ops_needs_writenotify(vma->vm_ops)) 1531*49b1b8d6SLorenzo Stoakes return true; 1532*49b1b8d6SLorenzo Stoakes 1533*49b1b8d6SLorenzo Stoakes /* The open routine did something to the protections that pgprot_modify 1534*49b1b8d6SLorenzo Stoakes * won't preserve? */ 1535*49b1b8d6SLorenzo Stoakes if (pgprot_val(vm_page_prot) != 1536*49b1b8d6SLorenzo Stoakes pgprot_val(vm_pgprot_modify(vm_page_prot, vma->vm_flags))) 1537*49b1b8d6SLorenzo Stoakes return false; 1538*49b1b8d6SLorenzo Stoakes 1539*49b1b8d6SLorenzo Stoakes /* 1540*49b1b8d6SLorenzo Stoakes * Do we need to track softdirty? hugetlb does not support softdirty 1541*49b1b8d6SLorenzo Stoakes * tracking yet. 1542*49b1b8d6SLorenzo Stoakes */ 1543*49b1b8d6SLorenzo Stoakes if (vma_soft_dirty_enabled(vma) && !is_vm_hugetlb_page(vma)) 1544*49b1b8d6SLorenzo Stoakes return true; 1545*49b1b8d6SLorenzo Stoakes 1546*49b1b8d6SLorenzo Stoakes /* Do we need write faults for uffd-wp tracking? */ 1547*49b1b8d6SLorenzo Stoakes if (userfaultfd_wp(vma)) 1548*49b1b8d6SLorenzo Stoakes return true; 1549*49b1b8d6SLorenzo Stoakes 1550*49b1b8d6SLorenzo Stoakes /* Can the mapping track the dirty pages? */ 1551*49b1b8d6SLorenzo Stoakes return vma_fs_can_writeback(vma); 1552*49b1b8d6SLorenzo Stoakes } 1553*49b1b8d6SLorenzo Stoakes 1554*49b1b8d6SLorenzo Stoakes unsigned long count_vma_pages_range(struct mm_struct *mm, 1555*49b1b8d6SLorenzo Stoakes unsigned long addr, unsigned long end) 1556*49b1b8d6SLorenzo Stoakes { 1557*49b1b8d6SLorenzo Stoakes VMA_ITERATOR(vmi, mm, addr); 1558*49b1b8d6SLorenzo Stoakes struct vm_area_struct *vma; 1559*49b1b8d6SLorenzo Stoakes unsigned long nr_pages = 0; 1560*49b1b8d6SLorenzo Stoakes 1561*49b1b8d6SLorenzo Stoakes for_each_vma_range(vmi, vma, end) { 1562*49b1b8d6SLorenzo Stoakes unsigned long vm_start = max(addr, vma->vm_start); 1563*49b1b8d6SLorenzo Stoakes unsigned long vm_end = min(end, vma->vm_end); 1564*49b1b8d6SLorenzo Stoakes 1565*49b1b8d6SLorenzo Stoakes nr_pages += PHYS_PFN(vm_end - vm_start); 1566*49b1b8d6SLorenzo Stoakes } 1567*49b1b8d6SLorenzo Stoakes 1568*49b1b8d6SLorenzo Stoakes return nr_pages; 1569*49b1b8d6SLorenzo Stoakes } 1570*49b1b8d6SLorenzo Stoakes 1571*49b1b8d6SLorenzo Stoakes static DEFINE_MUTEX(mm_all_locks_mutex); 1572*49b1b8d6SLorenzo Stoakes 1573*49b1b8d6SLorenzo Stoakes static void vm_lock_anon_vma(struct mm_struct *mm, struct anon_vma *anon_vma) 1574*49b1b8d6SLorenzo Stoakes { 1575*49b1b8d6SLorenzo Stoakes if (!test_bit(0, (unsigned long *) &anon_vma->root->rb_root.rb_root.rb_node)) { 1576*49b1b8d6SLorenzo Stoakes /* 1577*49b1b8d6SLorenzo Stoakes * The LSB of head.next can't change from under us 1578*49b1b8d6SLorenzo Stoakes * because we hold the mm_all_locks_mutex. 1579*49b1b8d6SLorenzo Stoakes */ 1580*49b1b8d6SLorenzo Stoakes down_write_nest_lock(&anon_vma->root->rwsem, &mm->mmap_lock); 1581*49b1b8d6SLorenzo Stoakes /* 1582*49b1b8d6SLorenzo Stoakes * We can safely modify head.next after taking the 1583*49b1b8d6SLorenzo Stoakes * anon_vma->root->rwsem. If some other vma in this mm shares 1584*49b1b8d6SLorenzo Stoakes * the same anon_vma we won't take it again. 1585*49b1b8d6SLorenzo Stoakes * 1586*49b1b8d6SLorenzo Stoakes * No need of atomic instructions here, head.next 1587*49b1b8d6SLorenzo Stoakes * can't change from under us thanks to the 1588*49b1b8d6SLorenzo Stoakes * anon_vma->root->rwsem. 1589*49b1b8d6SLorenzo Stoakes */ 1590*49b1b8d6SLorenzo Stoakes if (__test_and_set_bit(0, (unsigned long *) 1591*49b1b8d6SLorenzo Stoakes &anon_vma->root->rb_root.rb_root.rb_node)) 1592*49b1b8d6SLorenzo Stoakes BUG(); 1593*49b1b8d6SLorenzo Stoakes } 1594*49b1b8d6SLorenzo Stoakes } 1595*49b1b8d6SLorenzo Stoakes 1596*49b1b8d6SLorenzo Stoakes static void vm_lock_mapping(struct mm_struct *mm, struct address_space *mapping) 1597*49b1b8d6SLorenzo Stoakes { 1598*49b1b8d6SLorenzo Stoakes if (!test_bit(AS_MM_ALL_LOCKS, &mapping->flags)) { 1599*49b1b8d6SLorenzo Stoakes /* 1600*49b1b8d6SLorenzo Stoakes * AS_MM_ALL_LOCKS can't change from under us because 1601*49b1b8d6SLorenzo Stoakes * we hold the mm_all_locks_mutex. 1602*49b1b8d6SLorenzo Stoakes * 1603*49b1b8d6SLorenzo Stoakes * Operations on ->flags have to be atomic because 1604*49b1b8d6SLorenzo Stoakes * even if AS_MM_ALL_LOCKS is stable thanks to the 1605*49b1b8d6SLorenzo Stoakes * mm_all_locks_mutex, there may be other cpus 1606*49b1b8d6SLorenzo Stoakes * changing other bitflags in parallel to us. 1607*49b1b8d6SLorenzo Stoakes */ 1608*49b1b8d6SLorenzo Stoakes if (test_and_set_bit(AS_MM_ALL_LOCKS, &mapping->flags)) 1609*49b1b8d6SLorenzo Stoakes BUG(); 1610*49b1b8d6SLorenzo Stoakes down_write_nest_lock(&mapping->i_mmap_rwsem, &mm->mmap_lock); 1611*49b1b8d6SLorenzo Stoakes } 1612*49b1b8d6SLorenzo Stoakes } 1613*49b1b8d6SLorenzo Stoakes 1614*49b1b8d6SLorenzo Stoakes /* 1615*49b1b8d6SLorenzo Stoakes * This operation locks against the VM for all pte/vma/mm related 1616*49b1b8d6SLorenzo Stoakes * operations that could ever happen on a certain mm. This includes 1617*49b1b8d6SLorenzo Stoakes * vmtruncate, try_to_unmap, and all page faults. 1618*49b1b8d6SLorenzo Stoakes * 1619*49b1b8d6SLorenzo Stoakes * The caller must take the mmap_lock in write mode before calling 1620*49b1b8d6SLorenzo Stoakes * mm_take_all_locks(). The caller isn't allowed to release the 1621*49b1b8d6SLorenzo Stoakes * mmap_lock until mm_drop_all_locks() returns. 1622*49b1b8d6SLorenzo Stoakes * 1623*49b1b8d6SLorenzo Stoakes * mmap_lock in write mode is required in order to block all operations 1624*49b1b8d6SLorenzo Stoakes * that could modify pagetables and free pages without need of 1625*49b1b8d6SLorenzo Stoakes * altering the vma layout. It's also needed in write mode to avoid new 1626*49b1b8d6SLorenzo Stoakes * anon_vmas to be associated with existing vmas. 1627*49b1b8d6SLorenzo Stoakes * 1628*49b1b8d6SLorenzo Stoakes * A single task can't take more than one mm_take_all_locks() in a row 1629*49b1b8d6SLorenzo Stoakes * or it would deadlock. 1630*49b1b8d6SLorenzo Stoakes * 1631*49b1b8d6SLorenzo Stoakes * The LSB in anon_vma->rb_root.rb_node and the AS_MM_ALL_LOCKS bitflag in 1632*49b1b8d6SLorenzo Stoakes * mapping->flags avoid to take the same lock twice, if more than one 1633*49b1b8d6SLorenzo Stoakes * vma in this mm is backed by the same anon_vma or address_space. 1634*49b1b8d6SLorenzo Stoakes * 1635*49b1b8d6SLorenzo Stoakes * We take locks in following order, accordingly to comment at beginning 1636*49b1b8d6SLorenzo Stoakes * of mm/rmap.c: 1637*49b1b8d6SLorenzo Stoakes * - all hugetlbfs_i_mmap_rwsem_key locks (aka mapping->i_mmap_rwsem for 1638*49b1b8d6SLorenzo Stoakes * hugetlb mapping); 1639*49b1b8d6SLorenzo Stoakes * - all vmas marked locked 1640*49b1b8d6SLorenzo Stoakes * - all i_mmap_rwsem locks; 1641*49b1b8d6SLorenzo Stoakes * - all anon_vma->rwseml 1642*49b1b8d6SLorenzo Stoakes * 1643*49b1b8d6SLorenzo Stoakes * We can take all locks within these types randomly because the VM code 1644*49b1b8d6SLorenzo Stoakes * doesn't nest them and we protected from parallel mm_take_all_locks() by 1645*49b1b8d6SLorenzo Stoakes * mm_all_locks_mutex. 1646*49b1b8d6SLorenzo Stoakes * 1647*49b1b8d6SLorenzo Stoakes * mm_take_all_locks() and mm_drop_all_locks are expensive operations 1648*49b1b8d6SLorenzo Stoakes * that may have to take thousand of locks. 1649*49b1b8d6SLorenzo Stoakes * 1650*49b1b8d6SLorenzo Stoakes * mm_take_all_locks() can fail if it's interrupted by signals. 1651*49b1b8d6SLorenzo Stoakes */ 1652*49b1b8d6SLorenzo Stoakes int mm_take_all_locks(struct mm_struct *mm) 1653*49b1b8d6SLorenzo Stoakes { 1654*49b1b8d6SLorenzo Stoakes struct vm_area_struct *vma; 1655*49b1b8d6SLorenzo Stoakes struct anon_vma_chain *avc; 1656*49b1b8d6SLorenzo Stoakes VMA_ITERATOR(vmi, mm, 0); 1657*49b1b8d6SLorenzo Stoakes 1658*49b1b8d6SLorenzo Stoakes mmap_assert_write_locked(mm); 1659*49b1b8d6SLorenzo Stoakes 1660*49b1b8d6SLorenzo Stoakes mutex_lock(&mm_all_locks_mutex); 1661*49b1b8d6SLorenzo Stoakes 1662*49b1b8d6SLorenzo Stoakes /* 1663*49b1b8d6SLorenzo Stoakes * vma_start_write() does not have a complement in mm_drop_all_locks() 1664*49b1b8d6SLorenzo Stoakes * because vma_start_write() is always asymmetrical; it marks a VMA as 1665*49b1b8d6SLorenzo Stoakes * being written to until mmap_write_unlock() or mmap_write_downgrade() 1666*49b1b8d6SLorenzo Stoakes * is reached. 1667*49b1b8d6SLorenzo Stoakes */ 1668*49b1b8d6SLorenzo Stoakes for_each_vma(vmi, vma) { 1669*49b1b8d6SLorenzo Stoakes if (signal_pending(current)) 1670*49b1b8d6SLorenzo Stoakes goto out_unlock; 1671*49b1b8d6SLorenzo Stoakes vma_start_write(vma); 1672*49b1b8d6SLorenzo Stoakes } 1673*49b1b8d6SLorenzo Stoakes 1674*49b1b8d6SLorenzo Stoakes vma_iter_init(&vmi, mm, 0); 1675*49b1b8d6SLorenzo Stoakes for_each_vma(vmi, vma) { 1676*49b1b8d6SLorenzo Stoakes if (signal_pending(current)) 1677*49b1b8d6SLorenzo Stoakes goto out_unlock; 1678*49b1b8d6SLorenzo Stoakes if (vma->vm_file && vma->vm_file->f_mapping && 1679*49b1b8d6SLorenzo Stoakes is_vm_hugetlb_page(vma)) 1680*49b1b8d6SLorenzo Stoakes vm_lock_mapping(mm, vma->vm_file->f_mapping); 1681*49b1b8d6SLorenzo Stoakes } 1682*49b1b8d6SLorenzo Stoakes 1683*49b1b8d6SLorenzo Stoakes vma_iter_init(&vmi, mm, 0); 1684*49b1b8d6SLorenzo Stoakes for_each_vma(vmi, vma) { 1685*49b1b8d6SLorenzo Stoakes if (signal_pending(current)) 1686*49b1b8d6SLorenzo Stoakes goto out_unlock; 1687*49b1b8d6SLorenzo Stoakes if (vma->vm_file && vma->vm_file->f_mapping && 1688*49b1b8d6SLorenzo Stoakes !is_vm_hugetlb_page(vma)) 1689*49b1b8d6SLorenzo Stoakes vm_lock_mapping(mm, vma->vm_file->f_mapping); 1690*49b1b8d6SLorenzo Stoakes } 1691*49b1b8d6SLorenzo Stoakes 1692*49b1b8d6SLorenzo Stoakes vma_iter_init(&vmi, mm, 0); 1693*49b1b8d6SLorenzo Stoakes for_each_vma(vmi, vma) { 1694*49b1b8d6SLorenzo Stoakes if (signal_pending(current)) 1695*49b1b8d6SLorenzo Stoakes goto out_unlock; 1696*49b1b8d6SLorenzo Stoakes if (vma->anon_vma) 1697*49b1b8d6SLorenzo Stoakes list_for_each_entry(avc, &vma->anon_vma_chain, same_vma) 1698*49b1b8d6SLorenzo Stoakes vm_lock_anon_vma(mm, avc->anon_vma); 1699*49b1b8d6SLorenzo Stoakes } 1700*49b1b8d6SLorenzo Stoakes 1701*49b1b8d6SLorenzo Stoakes return 0; 1702*49b1b8d6SLorenzo Stoakes 1703*49b1b8d6SLorenzo Stoakes out_unlock: 1704*49b1b8d6SLorenzo Stoakes mm_drop_all_locks(mm); 1705*49b1b8d6SLorenzo Stoakes return -EINTR; 1706*49b1b8d6SLorenzo Stoakes } 1707*49b1b8d6SLorenzo Stoakes 1708*49b1b8d6SLorenzo Stoakes static void vm_unlock_anon_vma(struct anon_vma *anon_vma) 1709*49b1b8d6SLorenzo Stoakes { 1710*49b1b8d6SLorenzo Stoakes if (test_bit(0, (unsigned long *) &anon_vma->root->rb_root.rb_root.rb_node)) { 1711*49b1b8d6SLorenzo Stoakes /* 1712*49b1b8d6SLorenzo Stoakes * The LSB of head.next can't change to 0 from under 1713*49b1b8d6SLorenzo Stoakes * us because we hold the mm_all_locks_mutex. 1714*49b1b8d6SLorenzo Stoakes * 1715*49b1b8d6SLorenzo Stoakes * We must however clear the bitflag before unlocking 1716*49b1b8d6SLorenzo Stoakes * the vma so the users using the anon_vma->rb_root will 1717*49b1b8d6SLorenzo Stoakes * never see our bitflag. 1718*49b1b8d6SLorenzo Stoakes * 1719*49b1b8d6SLorenzo Stoakes * No need of atomic instructions here, head.next 1720*49b1b8d6SLorenzo Stoakes * can't change from under us until we release the 1721*49b1b8d6SLorenzo Stoakes * anon_vma->root->rwsem. 1722*49b1b8d6SLorenzo Stoakes */ 1723*49b1b8d6SLorenzo Stoakes if (!__test_and_clear_bit(0, (unsigned long *) 1724*49b1b8d6SLorenzo Stoakes &anon_vma->root->rb_root.rb_root.rb_node)) 1725*49b1b8d6SLorenzo Stoakes BUG(); 1726*49b1b8d6SLorenzo Stoakes anon_vma_unlock_write(anon_vma); 1727*49b1b8d6SLorenzo Stoakes } 1728*49b1b8d6SLorenzo Stoakes } 1729*49b1b8d6SLorenzo Stoakes 1730*49b1b8d6SLorenzo Stoakes static void vm_unlock_mapping(struct address_space *mapping) 1731*49b1b8d6SLorenzo Stoakes { 1732*49b1b8d6SLorenzo Stoakes if (test_bit(AS_MM_ALL_LOCKS, &mapping->flags)) { 1733*49b1b8d6SLorenzo Stoakes /* 1734*49b1b8d6SLorenzo Stoakes * AS_MM_ALL_LOCKS can't change to 0 from under us 1735*49b1b8d6SLorenzo Stoakes * because we hold the mm_all_locks_mutex. 1736*49b1b8d6SLorenzo Stoakes */ 1737*49b1b8d6SLorenzo Stoakes i_mmap_unlock_write(mapping); 1738*49b1b8d6SLorenzo Stoakes if (!test_and_clear_bit(AS_MM_ALL_LOCKS, 1739*49b1b8d6SLorenzo Stoakes &mapping->flags)) 1740*49b1b8d6SLorenzo Stoakes BUG(); 1741*49b1b8d6SLorenzo Stoakes } 1742*49b1b8d6SLorenzo Stoakes } 1743*49b1b8d6SLorenzo Stoakes 1744*49b1b8d6SLorenzo Stoakes /* 1745*49b1b8d6SLorenzo Stoakes * The mmap_lock cannot be released by the caller until 1746*49b1b8d6SLorenzo Stoakes * mm_drop_all_locks() returns. 1747*49b1b8d6SLorenzo Stoakes */ 1748*49b1b8d6SLorenzo Stoakes void mm_drop_all_locks(struct mm_struct *mm) 1749*49b1b8d6SLorenzo Stoakes { 1750*49b1b8d6SLorenzo Stoakes struct vm_area_struct *vma; 1751*49b1b8d6SLorenzo Stoakes struct anon_vma_chain *avc; 1752*49b1b8d6SLorenzo Stoakes VMA_ITERATOR(vmi, mm, 0); 1753*49b1b8d6SLorenzo Stoakes 1754*49b1b8d6SLorenzo Stoakes mmap_assert_write_locked(mm); 1755*49b1b8d6SLorenzo Stoakes BUG_ON(!mutex_is_locked(&mm_all_locks_mutex)); 1756*49b1b8d6SLorenzo Stoakes 1757*49b1b8d6SLorenzo Stoakes for_each_vma(vmi, vma) { 1758*49b1b8d6SLorenzo Stoakes if (vma->anon_vma) 1759*49b1b8d6SLorenzo Stoakes list_for_each_entry(avc, &vma->anon_vma_chain, same_vma) 1760*49b1b8d6SLorenzo Stoakes vm_unlock_anon_vma(avc->anon_vma); 1761*49b1b8d6SLorenzo Stoakes if (vma->vm_file && vma->vm_file->f_mapping) 1762*49b1b8d6SLorenzo Stoakes vm_unlock_mapping(vma->vm_file->f_mapping); 1763*49b1b8d6SLorenzo Stoakes } 1764*49b1b8d6SLorenzo Stoakes 1765*49b1b8d6SLorenzo Stoakes mutex_unlock(&mm_all_locks_mutex); 1766*49b1b8d6SLorenzo Stoakes } 1767