]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
mm/memory: move adjusting of address range to unmap_vmas()
authorDavid Hildenbrand (Arm) <david@kernel.org>
Fri, 27 Feb 2026 20:08:39 +0000 (21:08 +0100)
committerAndrew Morton <akpm@linux-foundation.org>
Sun, 5 Apr 2026 20:53:14 +0000 (13:53 -0700)
__zap_vma_range() has two callers, whereby zap_page_range_single_batched()
documents that the range must fit into the VMA range.

So move adjusting the range to unmap_vmas() where it is actually required
and add a safety check in __zap_vma_range() instead.  In unmap_vmas(),
we'd never expect to have empty ranges (otherwise, why have the vma in
there in the first place).

__zap_vma_range() will no longer be called with start == end, so cleanup
the function a bit.  While at it, simplify the overly long comment to its
core message.

We will no longer call uprobe_munmap() for start == end, which actually
seems to be the right thing to do.

Note that hugetlb_zap_begin()->...->adjust_range_if_pmd_sharing_possible()
cannot result in the range exceeding the vma range.

Link: https://lkml.kernel.org/r/20260227200848.114019-9-david@kernel.org
Signed-off-by: David Hildenbrand (Arm) <david@kernel.org>
Reviewed-by: Lorenzo Stoakes (Oracle) <ljs@kernel.org>
Cc: Alexander Gordeev <agordeev@linux.ibm.com>
Cc: Alexei Starovoitov <ast@kernel.org>
Cc: Alice Ryhl <aliceryhl@google.com>
Cc: Andrii Nakryiko <andrii@kernel.org>
Cc: Andy Lutomirski <luto@kernel.org>
Cc: Arnaldo Carvalho de Melo <acme@kernel.org>
Cc: Arnd Bergmann <arnd@arndb.de>
Cc: Arve <arve@android.com>
Cc: "Borislav Petkov (AMD)" <bp@alien8.de>
Cc: Carlos Llamas <cmllamas@google.com>
Cc: Christian Borntraeger <borntraeger@linux.ibm.com>
Cc: Christian Brauner <brauner@kernel.org>
Cc: Claudio Imbrenda <imbrenda@linux.ibm.com>
Cc: Daniel Borkman <daniel@iogearbox.net>
Cc: Dave Airlie <airlied@gmail.com>
Cc: David Ahern <dsahern@kernel.org>
Cc: David Rientjes <rientjes@google.com>
Cc: David S. Miller <davem@davemloft.net>
Cc: Dimitri Sivanich <dimitri.sivanich@hpe.com>
Cc: Eric Dumazet <edumazet@google.com>
Cc: Gerald Schaefer <gerald.schaefer@linux.ibm.com>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: Hartley Sweeten <hsweeten@visionengravers.com>
Cc: Heiko Carstens <hca@linux.ibm.com>
Cc: Ian Abbott <abbotti@mev.co.uk>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Jakub Kacinski <kuba@kernel.org>
Cc: Jani Nikula <jani.nikula@linux.intel.com>
Cc: Jann Horn <jannh@google.com>
Cc: Janosch Frank <frankja@linux.ibm.com>
Cc: Jarkko Sakkinen <jarkko@kernel.org>
Cc: Jason Gunthorpe <jgg@ziepe.ca>
Cc: Jonas Lahtinen <joonas.lahtinen@linux.intel.com>
Cc: Leon Romanovsky <leon@kernel.org>
Cc: Liam Howlett <liam.howlett@oracle.com>
Cc: Madhavan Srinivasan <maddy@linux.ibm.com>
Cc: Matthew Wilcox (Oracle) <willy@infradead.org>
Cc: Michael Ellerman <mpe@ellerman.id.au>
Cc: Michal Hocko <mhocko@suse.com>
Cc: Miguel Ojeda <ojeda@kernel.org>
Cc: Mike Rapoport <rppt@kernel.org>
Cc: Namhyung kim <namhyung@kernel.org>
Cc: Neal Cardwell <ncardwell@google.com>
Cc: Paolo Abeni <pabeni@redhat.com>
Cc: Pedro Falcato <pfalcato@suse.de>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Rodrigo Vivi <rodrigo.vivi@intel.com>
Cc: Shakeel Butt <shakeel.butt@linux.dev>
Cc: Suren Baghdasaryan <surenb@google.com>
Cc: Todd Kjos <tkjos@android.com>
Cc: Tvrtko Ursulin <tursulin@ursulin.net>
Cc: Vasily Gorbik <gor@linux.ibm.com>
Cc: Vincenzo Frascino <vincenzo.frascino@arm.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
mm/memory.c

index ca718b0029c44d1c6eafff5feade96d51333041f..7e5d52534ee90cc5c0ecdbfbee4ea8a71045f586 100644 (file)
@@ -2073,44 +2073,28 @@ static void unmap_page_range(struct mmu_gather *tlb, struct vm_area_struct *vma,
        tlb_end_vma(tlb, vma);
 }
 
-
-static void __zap_vma_range(struct mmu_gather *tlb,
-               struct vm_area_struct *vma, unsigned long start_addr,
-               unsigned long end_addr, struct zap_details *details)
+static void __zap_vma_range(struct mmu_gather *tlb, struct vm_area_struct *vma,
+               unsigned long start, unsigned long end,
+               struct zap_details *details)
 {
-       unsigned long start = max(vma->vm_start, start_addr);
-       unsigned long end;
-
-       if (start >= vma->vm_end)
-               return;
-       end = min(vma->vm_end, end_addr);
-       if (end <= vma->vm_start)
-               return;
+       VM_WARN_ON_ONCE(start >= end || !range_in_vma(vma, start, end));
 
        if (vma->vm_file)
                uprobe_munmap(vma, start, end);
 
-       if (start != end) {
-               if (unlikely(is_vm_hugetlb_page(vma))) {
-                       /*
-                        * It is undesirable to test vma->vm_file as it
-                        * should be non-null for valid hugetlb area.
-                        * However, vm_file will be NULL in the error
-                        * cleanup path of mmap_region. When
-                        * hugetlbfs ->mmap method fails,
-                        * mmap_region() nullifies vma->vm_file
-                        * before calling this function to clean up.
-                        * Since no pte has actually been setup, it is
-                        * safe to do nothing in this case.
-                        */
-                       if (vma->vm_file) {
-                               zap_flags_t zap_flags = details ?
-                                   details->zap_flags : 0;
-                               __unmap_hugepage_range(tlb, vma, start, end,
-                                                            NULL, zap_flags);
-                       }
-               } else
-                       unmap_page_range(tlb, vma, start, end, details);
+       if (unlikely(is_vm_hugetlb_page(vma))) {
+               zap_flags_t zap_flags = details ? details->zap_flags : 0;
+
+               /*
+                * vm_file will be NULL when we fail early while instantiating
+                * a new mapping. In this case, no pages were mapped yet and
+                * there is nothing to do.
+                */
+               if (!vma->vm_file)
+                       return;
+               __unmap_hugepage_range(tlb, vma, start, end, NULL, zap_flags);
+       } else {
+               unmap_page_range(tlb, vma, start, end, details);
        }
 }
 
@@ -2174,8 +2158,9 @@ void unmap_vmas(struct mmu_gather *tlb, struct unmap_desc *unmap)
                                unmap->vma_start, unmap->vma_end);
        mmu_notifier_invalidate_range_start(&range);
        do {
-               unsigned long start = unmap->vma_start;
-               unsigned long end = unmap->vma_end;
+               unsigned long start = max(vma->vm_start, unmap->vma_start);
+               unsigned long end = min(vma->vm_end, unmap->vma_end);
+
                hugetlb_zap_begin(vma, &start, &end);
                __zap_vma_range(tlb, vma, start, end, &details);
                hugetlb_zap_end(vma, &details);
@@ -2204,6 +2189,9 @@ void zap_page_range_single_batched(struct mmu_gather *tlb,
 
        VM_WARN_ON_ONCE(!tlb || tlb->mm != vma->vm_mm);
 
+       if (unlikely(!size))
+               return;
+
        mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, vma->vm_mm,
                                address, end);
        hugetlb_zap_begin(vma, &range.start, &range.end);