]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
mm/vmalloc: Warn on improper use of vunmap_range()
authorRyan Roberts <ryan.roberts@arm.com>
Tue, 22 Apr 2025 08:18:15 +0000 (09:18 +0100)
committerWill Deacon <will@kernel.org>
Fri, 9 May 2025 12:43:07 +0000 (13:43 +0100)
A call to vmalloc_huge() may cause memory blocks to be mapped at pmd or
pud level. But it is possible to subsequently call vunmap_range() on a
sub-range of the mapped memory, which partially overlaps a pmd or pud.
In this case, vmalloc unmaps the entire pmd or pud so that the
no-overlapping portion is also unmapped. Clearly that would have a bad
outcome, but it's not something that any callers do today as far as I
can tell. So I guess it's just expected that callers will not do this.

However, it would be useful to know if this happened in future; let's
add a warning to cover the eventuality.

Reviewed-by: Anshuman Khandual <anshuman.khandual@arm.com>
Reviewed-by: Catalin Marinas <catalin.marinas@arm.com>
Reviewed-by: Uladzislau Rezki (Sony) <urezki@gmail.com>
Signed-off-by: Ryan Roberts <ryan.roberts@arm.com>
Tested-by: Luiz Capitulino <luizcap@redhat.com>
Link: https://lore.kernel.org/r/20250422081822.1836315-8-ryan.roberts@arm.com
Signed-off-by: Will Deacon <will@kernel.org>
mm/vmalloc.c

index 3ed720a787ecd88a3eba63549c4f66ca1b4f2b23..d60d3a29d149517b6c5f7aee5d79b89d62bc5c43 100644 (file)
@@ -374,8 +374,10 @@ static void vunmap_pmd_range(pud_t *pud, unsigned long addr, unsigned long end,
                if (cleared || pmd_bad(*pmd))
                        *mask |= PGTBL_PMD_MODIFIED;
 
-               if (cleared)
+               if (cleared) {
+                       WARN_ON(next - addr < PMD_SIZE);
                        continue;
+               }
                if (pmd_none_or_clear_bad(pmd))
                        continue;
                vunmap_pte_range(pmd, addr, next, mask);
@@ -399,8 +401,10 @@ static void vunmap_pud_range(p4d_t *p4d, unsigned long addr, unsigned long end,
                if (cleared || pud_bad(*pud))
                        *mask |= PGTBL_PUD_MODIFIED;
 
-               if (cleared)
+               if (cleared) {
+                       WARN_ON(next - addr < PUD_SIZE);
                        continue;
+               }
                if (pud_none_or_clear_bad(pud))
                        continue;
                vunmap_pmd_range(pud, addr, next, mask);