From: Ye Liu Replace open-coded & ~(align - 1) with the standard ALIGN_DOWN() macro, matching the existing ALIGN() usage on the adjacent vmalloc_start line. No functional change. Signed-off-by: Ye Liu --- mm/vmalloc.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/mm/vmalloc.c b/mm/vmalloc.c index ee722162da7c..56f0bfc3147c 100644 --- a/mm/vmalloc.c +++ b/mm/vmalloc.c @@ -5166,13 +5166,13 @@ pvm_find_va_enclose_addr(unsigned long addr) static unsigned long pvm_determine_end_from_reverse(struct vmap_area **va, unsigned long align) { - unsigned long vmalloc_end = VMALLOC_END & ~(align - 1); + unsigned long vmalloc_end = ALIGN_DOWN(VMALLOC_END, align); unsigned long addr; if (likely(*va)) { list_for_each_entry_from_reverse((*va), &free_vmap_area_list, list) { - addr = min((*va)->va_end & ~(align - 1), vmalloc_end); + addr = min(ALIGN_DOWN((*va)->va_end, align), vmalloc_end); if ((*va)->va_start < addr) return addr; } @@ -5211,7 +5211,7 @@ struct vm_struct **pcpu_get_vm_areas(const unsigned long *offsets, size_t align, gfp_t gfp) { const unsigned long vmalloc_start = ALIGN(VMALLOC_START, align); - const unsigned long vmalloc_end = VMALLOC_END & ~(align - 1); + const unsigned long vmalloc_end = ALIGN_DOWN(VMALLOC_END, align); struct vmap_area **vas, *va; struct vm_struct **vms; int area, area2, last_area, term_area; -- 2.25.1 From: Ye Liu Extract the repeated pattern of returning a vmap_area to the free tree with KASAN shadow release into a pcpu_release_va() helper, used by both the recovery and err_free_shadow error paths in pcpu_get_vm_areas(). No functional change. Signed-off-by: Ye Liu --- mm/vmalloc.c | 38 +++++++++++++++++++++----------------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/mm/vmalloc.c b/mm/vmalloc.c index 56f0bfc3147c..1f6ef2530e9e 100644 --- a/mm/vmalloc.c +++ b/mm/vmalloc.c @@ -5181,6 +5181,24 @@ pvm_determine_end_from_reverse(struct vmap_area **va, unsigned long align) return 0; } +/* + * Return a vmap_area to the free tree and release its KASAN shadow. + * Must be called with free_vmap_area_lock held. + */ +static void pcpu_release_va(struct vmap_area *va) +{ + unsigned long orig_start = va->va_start; + unsigned long orig_end = va->va_end; + struct vmap_area *merged; + + merged = merge_or_add_vmap_area_augment(va, &free_vmap_area_root, + &free_vmap_area_list); + if (merged) + kasan_release_vmalloc(orig_start, orig_end, + merged->va_start, merged->va_end, + KASAN_VMALLOC_PAGE_RANGE | KASAN_VMALLOC_TLB_FLUSH); +} + /** * pcpu_get_vm_areas - allocate vmalloc areas for percpu allocator * @offsets: array containing offset of each area @@ -5215,7 +5233,7 @@ struct vm_struct **pcpu_get_vm_areas(const unsigned long *offsets, struct vmap_area **vas, *va; struct vm_struct **vms; int area, area2, last_area, term_area; - unsigned long base, start, size, end, last_end, orig_start, orig_end; + unsigned long base, start, size, end, last_end; bool purged = false; /* verify parameters and allocate data structures */ @@ -5382,14 +5400,7 @@ struct vm_struct **pcpu_get_vm_areas(const unsigned long *offsets, * and when pcpu_get_vm_areas() is success. */ while (area--) { - orig_start = vas[area]->va_start; - orig_end = vas[area]->va_end; - va = merge_or_add_vmap_area_augment(vas[area], &free_vmap_area_root, - &free_vmap_area_list); - if (va) - kasan_release_vmalloc(orig_start, orig_end, - va->va_start, va->va_end, - KASAN_VMALLOC_PAGE_RANGE | KASAN_VMALLOC_TLB_FLUSH); + pcpu_release_va(vas[area]); vas[area] = NULL; } @@ -5433,14 +5444,7 @@ struct vm_struct **pcpu_get_vm_areas(const unsigned long *offsets, * being able to tolerate this case. */ for (area = 0; area < nr_vms; area++) { - orig_start = vas[area]->va_start; - orig_end = vas[area]->va_end; - va = merge_or_add_vmap_area_augment(vas[area], &free_vmap_area_root, - &free_vmap_area_list); - if (va) - kasan_release_vmalloc(orig_start, orig_end, - va->va_start, va->va_end, - KASAN_VMALLOC_PAGE_RANGE | KASAN_VMALLOC_TLB_FLUSH); + pcpu_release_va(vas[area]); vas[area] = NULL; kfree(vms[area]); } -- 2.25.1 From: Ye Liu The warning message referenced vmalloc_node_range but alloc_vmap_area is a generic allocator called from multiple paths, not just __vmalloc_node_range. Use __func__ and drop the inaccurate "Address range restricted" phrasing. Signed-off-by: Ye Liu --- mm/vmalloc.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mm/vmalloc.c b/mm/vmalloc.c index 1f6ef2530e9e..ed4c4f7054c3 100644 --- a/mm/vmalloc.c +++ b/mm/vmalloc.c @@ -2211,8 +2211,8 @@ static struct vmap_area *alloc_vmap_area(unsigned long size, } if (!(gfp_mask & __GFP_NOWARN) && printk_ratelimit()) - pr_warn("vmalloc_node_range for size %lu failed: Address range restricted to %#lx - %#lx\n", - size, vstart, vend); + pr_warn("%s: size %lu failed, range %#lx - %#lx\n", + __func__, size, vstart, vend); out_free_va: kmem_cache_free(vmap_area_cachep, va); -- 2.25.1 From: Ye Liu Collapse the if/return pattern into a direct boolean expression. No functional change. Signed-off-by: Ye Liu --- mm/vmalloc.c | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/mm/vmalloc.c b/mm/vmalloc.c index ed4c4f7054c3..d076ef22b84a 100644 --- a/mm/vmalloc.c +++ b/mm/vmalloc.c @@ -1100,10 +1100,7 @@ decode_vn_id(unsigned int val) static bool is_vn_id_valid(unsigned int node_id) { - if (node_id < nr_vmap_nodes) - return true; - - return false; + return node_id < nr_vmap_nodes; } static __always_inline unsigned long -- 2.25.1 From: Ye Liu vmap_pool->len is protected by pool_lock, as done in node_pool_add_va() and node_pool_del_va(). decay_va_pool_node() reads and clears len outside of the lock, which can leave it inconsistent with the pool list under concurrent access. Move both into the same critical section that detaches the pool list. Signed-off-by: Ye Liu --- mm/vmalloc.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/mm/vmalloc.c b/mm/vmalloc.c index d076ef22b84a..e24d04d2f4a0 100644 --- a/mm/vmalloc.c +++ b/mm/vmalloc.c @@ -2296,10 +2296,9 @@ decay_va_pool_node(struct vmap_node *vn, bool full_decay) /* Detach the pool, so no-one can access it. */ spin_lock(&vn->pool_lock); list_replace_init(&vn->pool[i].head, &tmp_list); - spin_unlock(&vn->pool_lock); - pool_len = n_decay = vn->pool[i].len; WRITE_ONCE(vn->pool[i].len, 0); + spin_unlock(&vn->pool_lock); /* Decay a pool by ~25% out of left objects. */ if (!full_decay) -- 2.25.1 From: Ye Liu When the multi-node array allocation fails, vmap_init_nodes() falls back to a single vmap node. Report the requested node count and the fallback, and downgrade the message from error to warning since the kernel continues to function normally. Signed-off-by: Ye Liu --- mm/vmalloc.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mm/vmalloc.c b/mm/vmalloc.c index e24d04d2f4a0..03cce37466d1 100644 --- a/mm/vmalloc.c +++ b/mm/vmalloc.c @@ -5701,7 +5701,7 @@ static void vmap_init_nodes(void) nr_vmap_nodes = n; vmap_nodes = vn; } else { - pr_err("Failed to allocate an array. Disable a node layer\n"); + pr_warn("Failed to allocate %d vmap nodes, falling back to one node\n", n); } } #endif -- 2.25.1 From: Ye Liu Invert the WARN_ON_ONCE() check and return early to avoid the double negation. No functional change. Signed-off-by: Ye Liu --- mm/vmalloc.c | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/mm/vmalloc.c b/mm/vmalloc.c index 03cce37466d1..66206de44267 100644 --- a/mm/vmalloc.c +++ b/mm/vmalloc.c @@ -5638,13 +5638,14 @@ static void __init vmap_insert_free_area(unsigned long start, unsigned long end) { struct vmap_area *free = kmem_cache_zalloc(vmap_area_cachep, GFP_NOWAIT); - if (!WARN_ON_ONCE(!free)) { - free->va_start = start; - free->va_end = end; - insert_vmap_area_augment(free, NULL, - &free_vmap_area_root, - &free_vmap_area_list); - } + if (WARN_ON_ONCE(!free)) + return; + + free->va_start = start; + free->va_end = end; + insert_vmap_area_augment(free, NULL, + &free_vmap_area_root, + &free_vmap_area_list); } static void __init vmap_init_free_space(void) -- 2.25.1 From: Ye Liu Use a local variable to avoid the repeated casts and compare the addresses directly instead of relying on unsigned subtraction, which can underflow. No functional change. Signed-off-by: Ye Liu --- mm/vmalloc.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/mm/vmalloc.c b/mm/vmalloc.c index 66206de44267..7eec038b8990 100644 --- a/mm/vmalloc.c +++ b/mm/vmalloc.c @@ -5661,14 +5661,15 @@ static void __init vmap_init_free_space(void) * |<--------------------------------->| */ for (busy = vmlist; busy; busy = busy->next) { - if ((unsigned long) busy->addr - vmap_start > 0) - vmap_insert_free_area(vmap_start, - (unsigned long) busy->addr); + unsigned long addr = (unsigned long) busy->addr; - vmap_start = (unsigned long) busy->addr + busy->size; + if (addr > vmap_start) + vmap_insert_free_area(vmap_start, addr); + + vmap_start = addr + busy->size; } - if (vmap_end - vmap_start > 0) + if (vmap_start < vmap_end) vmap_insert_free_area(vmap_start, vmap_end); } -- 2.25.1