From: Wen Jiang vrealloc() frees the tail pages of a shrinking area unless its page_order is non-zero, as partial freeing would require splitting a huge mapping, and is_vm_area_hugepages() reports huge mappings from page_order as well. Both rely on a huge vmalloc area being mapped either entirely with blocks of 1 << page_order pages or entirely with base pages. A follow-up change lets a huge vmalloc area fall back to smaller block sizes for part of the area. page_order then has to be the smallest order used, so that the page array can still be walked in steps of 1 << page_order. It can be 0 for an area that still contains PMD or contiguous PTE mappings. Add an internal VM_HAS_HUGE_VMAP flag, set when at least part of an area is mapped with entries larger than PAGE_SIZE, and use it in vrealloc() and is_vm_area_hugepages() instead of page_order. Together with page_order it describes the layout: !VM_HAS_HUGE_VMAP base pages only VM_HAS_HUGE_VMAP, page_order == 0 huge mappings plus base pages VM_HAS_HUGE_VMAP, page_order > 0 huge mappings only For vrealloc() and the powerpc set_memory_*() guard, any huge mapping is reason enough to back off. alloc_large_system_hash() only uses the result for its boot message. set_vm_area_page_order() sets the flag for a non-zero order, so for now the flag is set exactly when page_order is non-zero and there is no functional change. Like page_order, the flag is only set by the vmalloc allocation path. Huge mappings created by vmap() or ioremap() are tracked by neither. Let kho_preserve_vmalloc() accept the new flag, as it rejects any flag it does not know about. Suggested-by: Barry Song Assisted-by: LLM Signed-off-by: Wen Jiang --- include/linux/vmalloc.h | 7 ++++--- kernel/liveupdate/kexec_handover.c | 3 ++- mm/vmalloc.c | 9 ++++++--- 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/include/linux/vmalloc.h b/include/linux/vmalloc.h index 034a693777ca0..31be61ad88550 100644 --- a/include/linux/vmalloc.h +++ b/include/linux/vmalloc.h @@ -40,6 +40,7 @@ struct iov_iter; /* in uio.h */ #endif #define VM_SPARSE 0x00001000 /* sparse vm_area. not all pages are present. */ #define VM_REQUIRE_HUGE_VMAP 0x00002000 /* huge page mapping or nothing */ +#define VM_HAS_HUGE_VMAP 0x00004000 /* part of the area has huge mappings */ /* bits [20..32] reserved for arch specific ioremap internals */ @@ -265,13 +266,13 @@ static inline bool is_vm_area_hugepages(const void *addr) * This may not 100% tell if the area is mapped with > PAGE_SIZE * page table entries, if for some reason the architecture indicates * larger sizes are available but decides not to use them, nothing - * prevents that. This only indicates the size of the physical page - * allocated in the vmalloc layer. + * prevents that. This only indicates that the vmalloc layer mapped + * at least part of the area with such entries. */ #ifdef CONFIG_HAVE_ARCH_HUGE_VMALLOC struct vm_struct *area = find_vm_area(addr); - return area && area->page_order > 0; + return area && (area->flags & VM_HAS_HUGE_VMAP); #else return false; #endif diff --git a/kernel/liveupdate/kexec_handover.c b/kernel/liveupdate/kexec_handover.c index 42c6609a1766a..c2d5823b498a3 100644 --- a/kernel/liveupdate/kexec_handover.c +++ b/kernel/liveupdate/kexec_handover.c @@ -1288,7 +1288,8 @@ void kho_unpreserve_pages(struct page *page, unsigned long nr_pages) EXPORT_SYMBOL_GPL(kho_unpreserve_pages); /* vmalloc flags KHO supports */ -#define KHO_VMALLOC_SUPPORTED_FLAGS (VM_ALLOC | VM_ALLOW_HUGE_VMAP) +#define KHO_VMALLOC_SUPPORTED_FLAGS (VM_ALLOC | VM_ALLOW_HUGE_VMAP | \ + VM_HAS_HUGE_VMAP) /* KHO internal flags for vmalloc preservations */ #define KHO_VMALLOC_ALLOC 0x0001 diff --git a/mm/vmalloc.c b/mm/vmalloc.c index eb28dbd29c2e1..595b07a6cc3d1 100644 --- a/mm/vmalloc.c +++ b/mm/vmalloc.c @@ -3222,6 +3222,8 @@ static inline void set_vm_area_page_order(struct vm_struct *vm, unsigned int ord { #ifdef CONFIG_HAVE_ARCH_HUGE_VMALLOC vm->page_order = order; + if (order) + vm->flags |= VM_HAS_HUGE_VMAP; #else BUG_ON(order != 0); #endif @@ -4613,7 +4615,7 @@ void *vrealloc_node_align_noprof(const void *p, size_t size, unsigned long align /* * Free tail pages when shrink crosses a page boundary. * - * Skip huge page allocations (page_order > 0) as partial + * Skip areas with huge mappings (VM_HAS_HUGE_VMAP) as partial * freeing would require splitting. * * Skip VM_FLUSH_RESET_PERMS, as direct-map permissions must @@ -4629,8 +4631,9 @@ void *vrealloc_node_align_noprof(const void *p, size_t size, unsigned long align * GFP_KERNEL, which could trigger a recursive deadlock * if we are under filesystem or I/O reclaim. */ - if (new_nr_pages < vm->nr_pages && !vm_area_page_order(vm) && - !(vm->flags & (VM_FLUSH_RESET_PERMS | VM_USERMAP)) && + if (new_nr_pages < vm->nr_pages && + !(vm->flags & (VM_HAS_HUGE_VMAP | VM_FLUSH_RESET_PERMS | + VM_USERMAP)) && gfp_has_io_fs(flags)) { unsigned long addr = (unsigned long)kasan_reset_tag(p); unsigned long old_nr_pages = vm->nr_pages; -- 2.34.1