From: Baoquan He VM_SPARSE areas are created with get_vm_area() and filled in by the caller via vm_area_map_pages(), so the caller need to allocate the pages, keep the struct page array and free everything by itself. Add vm_area_grow_pages() and vm_area_shrink_pages() for VM_SPARSE | VM_ALLOC areas. Pages come from the page allocator and are tracked in area->pages, so the area is released with a single vfree(). The backed pages always start at the beginning of the area: growing maps [nr_pages, end) and shrinking unmaps and frees [end, nr_pages). Callers serialize the two helpers. nr_pages keeps its existing meaning of "number of backed pages", as with vrealloc() and vread_iter(), and is updated under the vmap node's busy.lock so /proc/vmallocinfo never sees a page that is already gone. Only areas populated through vm_area_map_pages() stay a memory hole. vm_area_map_pages() now rejects VM_ALLOC areas, because vfree() owns their pages, to avoid double frees; free_vm_area() warns for the same combination but not for plain VM_ALLOC areas. Suggested-by: Chris Li Signed-off-by: Baoquan He --- include/linux/vmalloc.h | 2 + mm/vmalloc.c | 171 +++++++++++++++++++++++++++++++++++++--- 2 files changed, 160 insertions(+), 13 deletions(-) diff --git a/include/linux/vmalloc.h b/include/linux/vmalloc.h index 034a693777ca..9f40edcddfa8 100644 --- a/include/linux/vmalloc.h +++ b/include/linux/vmalloc.h @@ -296,6 +296,8 @@ int vm_area_map_pages(struct vm_struct *area, unsigned long start, unsigned long end, struct page **pages); void vm_area_unmap_pages(struct vm_struct *area, unsigned long start, unsigned long end); +int vm_area_grow_pages(struct vm_struct *area, unsigned long end, gfp_t gfp); +void vm_area_shrink_pages(struct vm_struct *area, unsigned long end); void vunmap_range(unsigned long addr, unsigned long end); static inline void set_vm_flush_reset_perms(void *addr) diff --git a/mm/vmalloc.c b/mm/vmalloc.c index ee722162da7c..4a8c35b958bb 100644 --- a/mm/vmalloc.c +++ b/mm/vmalloc.c @@ -788,6 +788,8 @@ static int check_sparse_vm_area(struct vm_struct *area, unsigned long start, return -EINVAL; if (WARN_ON_ONCE(!(area->flags & VM_SPARSE))) return -EINVAL; + if (WARN_ON_ONCE(area->flags & VM_ALLOC)) + return -EINVAL; if ((end - start) >> PAGE_SHIFT > totalram_pages()) return -E2BIG; if (start < (unsigned long)area->addr || @@ -4174,6 +4176,136 @@ static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask, return NULL; } +/** + * vm_area_grow_pages - back more of a sparse area with pages + * @area: area created with %VM_SPARSE | %VM_ALLOC + * @end: end of the range to back, %PAGE_SIZE aligned + * @gfp: gfp flags for the page allocation + * + * Allocate and map pages for [@area->nr_pages << PAGE_SHIFT, @end). The + * pages are zeroed when %__GFP_ZERO is set. Pages are always backed from + * the start of the area, so a grow only extends that range. An @end that + * is already backed is a no-op. On failure nothing is mapped and + * @area->nr_pages is unchanged. + * + * The area must be released with vfree(@area->addr). Callers must + * serialize calls to this function. + * + * Return: 0 on success, -errno on failure. + */ +int vm_area_grow_pages(struct vm_struct *area, unsigned long end, gfp_t gfp) +{ + unsigned long start = (unsigned long)area->nr_pages << PAGE_SHIFT; + unsigned long addr = (unsigned long)area->addr; + unsigned long nr_pages, nr_allocated, nr_end, cap; + gfp_t nested_gfp = (gfp & GFP_RECLAIM_MASK) | __GFP_ZERO; + struct vmap_node *vn; + struct page **pages; + unsigned int flags; + int ret; + + might_sleep(); + + if (WARN_ON_ONCE(!(area->flags & VM_ALLOC) || + !(area->flags & VM_SPARSE))) + return -EINVAL; + if (WARN_ON_ONCE(!IS_ALIGNED(end, PAGE_SIZE))) + return -EINVAL; + if (WARN_ON_ONCE(end > get_vm_area_size(area))) + return -EINVAL; + if (end <= start) + return 0; + if ((end - start) >> PAGE_SHIFT > totalram_pages()) + return -E2BIG; + + nr_end = end >> PAGE_SHIFT; + cap = roundup_pow_of_two(nr_end); + pages = kvrealloc(area->pages, cap * sizeof(*area->pages), + nested_gfp); + if (!pages) + return -ENOMEM; + area->pages = pages; + + if (!(gfp & (GFP_DMA | GFP_DMA32))) + gfp |= __GFP_HIGHMEM; + + nr_pages = (end - start) >> PAGE_SHIFT; + nr_allocated = vm_area_alloc_pages(vmalloc_gfp_adjust(gfp, 0), + NUMA_NO_NODE, 0, nr_pages, + area->pages + (start >> PAGE_SHIFT)); + if (nr_allocated != nr_pages) { + vm_area_free_pages(area, start >> PAGE_SHIFT, + (start >> PAGE_SHIFT) + nr_allocated); + return -ENOMEM; + } + + flags = memalloc_apply_gfp_scope(gfp); + ret = __vmap_pages_range(addr + start, addr + end, PAGE_KERNEL, + area->pages + (start >> PAGE_SHIFT), + PAGE_SHIFT, nested_gfp); + memalloc_restore_scope(flags); + if (ret) + goto free_pages; + + kasan_unpoison_vmalloc((void *)(addr + start), end - start, + KASAN_VMALLOC_PROT_NORMAL | + KASAN_VMALLOC_KEEP_TAG); + + /* + * Publish under busy.lock, like clear_vm_uninitialized_flag(): + * show_busy_info() reads nr_pages and pages[] under that lock. + */ + vn = addr_to_node(addr); + spin_lock(&vn->busy.lock); + WRITE_ONCE(area->nr_pages, end >> PAGE_SHIFT); + spin_unlock(&vn->busy.lock); + return 0; + +free_pages: + vunmap_range(addr + start, addr + end); + vm_area_free_pages(area, start >> PAGE_SHIFT, end >> PAGE_SHIFT); + return -ENOMEM; +} + +/** + * vm_area_shrink_pages - release the pages backing the end of a sparse area + * @area: area created with %VM_SPARSE | %VM_ALLOC + * @end: new end of the backed range, %PAGE_SIZE aligned + * + * Unmap and free pages for [@end, @area->nr_pages << PAGE_SHIFT). The + * released range can be backed again with vm_area_grow_pages(). Callers + * must serialize calls to this function. + */ +void vm_area_shrink_pages(struct vm_struct *area, unsigned long end) +{ + unsigned long start = (unsigned long)area->nr_pages << PAGE_SHIFT; + unsigned long addr = (unsigned long)area->addr; + struct vmap_node *vn; + + might_sleep(); + + if (WARN_ON_ONCE(!(area->flags & VM_ALLOC) || + !(area->flags & VM_SPARSE))) + return; + if (WARN_ON_ONCE(!IS_ALIGNED(end, PAGE_SIZE))) + return; + if (end >= start) + return; + + /* + * Lower nr_pages under busy.lock before the pages go away: readers + * walk pages[] up to nr_pages under that lock. + */ + vn = addr_to_node(addr); + spin_lock(&vn->busy.lock); + WRITE_ONCE(area->nr_pages, end >> PAGE_SHIFT); + spin_unlock(&vn->busy.lock); + + kasan_poison_vmalloc((void *)(addr + end), start - end); + vunmap_range(addr + end, addr + start); + vm_area_free_pages(area, end >> PAGE_SHIFT, start >> PAGE_SHIFT); +} + /* * See __vmalloc_node_range() for a clear list of supported vmalloc flags. * This gfp lists all flags currently passed through vmalloc. Currently, @@ -4952,18 +5084,21 @@ long vread_iter(struct iov_iter *iter, const char *addr, size_t count) smp_rmb(); vaddr = (char *) va->va_start; - if (vm) + if (vm) { /* - * For VM_ALLOC areas, use nr_pages rather than - * get_vm_area_size() because vrealloc() may shrink - * the mapping without updating area->size. Other - * mapping types (vmap, ioremap) don't set nr_pages. + * Use nr_pages for VM_ALLOC areas: vrealloc() may + * shrink them without updating area->size, and a + * VM_SPARSE area is backed lazily, so its zero + * prefix means nothing is mapped. */ - size = (vm->flags & VM_ALLOC && vm->nr_pages) ? - (vm->nr_pages << PAGE_SHIFT) : - get_vm_area_size(vm); - else + if ((vm->flags & VM_ALLOC) && + (vm->nr_pages || (vm->flags & VM_SPARSE))) + size = vm->nr_pages << PAGE_SHIFT; + else + size = get_vm_area_size(vm); + } else { size = va_size(va); + } if (addr >= vaddr + size) goto next_va; @@ -4983,12 +5118,18 @@ long vread_iter(struct iov_iter *iter, const char *addr, size_t count) if (n > remains) n = remains; - if (flags & VMAP_RAM) + if (flags & VMAP_RAM) { copied = vmap_ram_vread_iter(iter, addr, n, flags); - else if (!(vm && (vm->flags & (VM_IOREMAP | VM_SPARSE)))) - copied = aligned_vread_iter(iter, addr, n); - else /* IOREMAP | SPARSE area is treated as memory hole */ + } else if (vm && (vm->flags & VM_IOREMAP)) { + /* Not backed by struct pages. */ + copied = zero_iter(iter, n); + } else if (vm && (vm->flags & VM_SPARSE) && + !(vm->flags & VM_ALLOC)) { + /* Populated by the caller; treat as a memory hole. */ copied = zero_iter(iter, n); + } else { + copied = aligned_vread_iter(iter, addr, n); + } addr += copied; remains -= copied; @@ -5107,6 +5248,10 @@ EXPORT_SYMBOL(remap_vmalloc_range); void free_vm_area(struct vm_struct *area) { struct vm_struct *ret; + + /* VM_SPARSE | VM_ALLOC areas own their pages and need vfree(). */ + WARN_ON_ONCE((area->flags & (VM_ALLOC | VM_SPARSE)) == + (VM_ALLOC | VM_SPARSE)); ret = remove_vm_area(area->addr); BUG_ON(ret != area); kfree(area); -- 2.54.0