[PATCH 09/11] mm: percpu: allocate and free local percpu vm area

Yang Shi
2026-04-29T17:09:43Z

Allocate local percpu vm area. The delta between the allocated addr
(chunk local base) and pcpu_local_base must be same with the delta
between chunk base and pcpu_base_addr. Each CPU's local percpu area
will be mapped to its own page table. This section of page table is not
shared between CPUs.

And free local percpu vm area. Also unmap from percpu page table.

Signed-off-by: Yang Shi <yang@os.amperecomputing.com>
---
include/linux/vmalloc.h | 3 ++
mm/percpu-internal.h | 1 +
mm/percpu-vm.c | 91 +++++++++++++++++++++++++++++++++++++++++
mm/vmalloc.c | 69 ++++++++++++++++++++++++++++---
4 files changed, 159 insertions(+), 5 deletions(-)

diff --git a/include/linux/vmalloc.h b/include/linux/vmalloc.h
index 3b02c0c6b371..4b53992a063c 100644
--- a/include/linux/vmalloc.h
+++ b/include/linux/vmalloc.h
@@ -311,6 +311,9 @@ struct vm_struct **pcpu_get_vm_areas(const unsigned long *offsets,
size_t align);

void pcpu_free_vm_areas(struct vm_struct **vms, int nr_vms);
+struct vm_struct *pcpu_get_local_vm_area(unsigned long hint,
+ int unit_size, size_t align);
+
# else
static inline struct vm_struct **
pcpu_get_vm_areas(const unsigned long *offsets,
diff --git a/mm/percpu-internal.h b/mm/percpu-internal.h
index 64b48b99ac06..2c560e44ee58 100644
--- a/mm/percpu-internal.h
+++ b/mm/percpu-internal.h
@@ -71,6 +71,7 @@ struct pcpu_chunk {
struct pcpu_block_md *md_blocks; /* metadata blocks */

void *data; /* chunk data */
+ void *local_data; /* chunk local vm */
bool immutable; /* no [de]population allowed */
bool isolated; /* isolated from active chunk
slots */
diff --git a/mm/percpu-vm.c b/mm/percpu-vm.c
index 4f5937090590..1e6b8fdcab71 100644
--- a/mm/percpu-vm.c
+++ b/mm/percpu-vm.c
@@ -9,6 +9,7 @@
* This is the default chunk allocator.
*/
#include "internal.h"
+#include "percpu-internal.h"

static struct page *pcpu_chunk_page(struct pcpu_chunk *chunk,
unsigned int cpu, int page_idx)
@@ -130,6 +131,11 @@ static void pcpu_pre_unmap_flush(struct pcpu_chunk *chunk,
flush_cache_vunmap(
pcpu_chunk_addr(chunk, pcpu_low_unit_cpu, page_start),
pcpu_chunk_addr(chunk, pcpu_high_unit_cpu, page_end));
+
+#ifdef CONFIG_HAVE_LOCAL_PER_CPU_MAP
+ flush_cache_vunmap((unsigned long)chunk->local_base + (page_start << PAGE_SHIFT),
+ (unsigned long)chunk->local_base + (page_end << PAGE_SHIFT));
+#endif
}

static void __pcpu_unmap_pages(unsigned long addr, int nr_pages)
@@ -137,6 +143,20 @@ static void __pcpu_unmap_pages(unsigned long addr, int nr_pages)
vunmap_range_noflush(addr, addr + (nr_pages << PAGE_SHIFT));
}

+#ifdef CONFIG_HAVE_LOCAL_PER_CPU_MAP
+static void __pcpu_unmap_pages_local(pgd_t *pgdir, unsigned long virt,
+ int nr_pages)
+{
+ __vunmap_range_noflush(pgdir, virt, virt + (nr_pages << PAGE_SHIFT));
+}
+#else
+static void __pcpu_unmap_pages_local(pgd_t *pgdir, unsigned long virt,
+ int nr_pages)
+{
+ return;
+}
+#endif
+
/**
* pcpu_unmap_pages - unmap pages out of a pcpu_chunk
* @chunk: chunk of interest
@@ -166,6 +186,10 @@ static void pcpu_unmap_pages(struct pcpu_chunk *chunk,
}
__pcpu_unmap_pages(pcpu_chunk_addr(chunk, cpu, page_start),
page_end - page_start);
+
+ __pcpu_unmap_pages_local(percpu_pgd[cpu],
+ (unsigned long)chunk->local_base + (page_start << PAGE_SHIFT),
+ page_end - page_start);
}
}

@@ -188,6 +212,12 @@ static void pcpu_post_unmap_tlb_flush(struct pcpu_chunk *chunk,
flush_tlb_kernel_range(
pcpu_chunk_addr(chunk, pcpu_low_unit_cpu, page_start),
pcpu_chunk_addr(chunk, pcpu_high_unit_cpu, page_end));
+
+#ifdef CONFIG_HAVE_LOCAL_PER_CPU_MAP
+ flush_tlb_kernel_range(
+ (unsigned long)chunk->local_base + (page_start << PAGE_SHIFT),
+ (unsigned long)chunk->local_base + (page_end << PAGE_SHIFT));
+#endif
}

static int __pcpu_map_pages(unsigned long addr, struct page **pages,
@@ -197,6 +227,32 @@ static int __pcpu_map_pages(unsigned long addr, struct page **pages,
PAGE_KERNEL, pages, PAGE_SHIFT, GFP_KERNEL);
}

+#ifdef CONFIG_HAVE_LOCAL_PER_CPU_MAP
+static int __pcpu_map_pages_local(pgd_t *pgdir, unsigned long virt, struct page **pages,
+ int nr_pages)
+{
+ unsigned int i;
+ int err = 0;
+
+ for (i = 0; i < nr_pages; i++) {
+ err = vmap_range_noflush(pgdir, virt, virt + PAGE_SIZE,
+ page_to_phys(pages[i]), PAGE_KERNEL, PAGE_SHIFT);
+ if (err)
+ return err;
+
+ virt += PAGE_SIZE;
+ }
+
+ return err;
+}
+#else
+static int __pcpu_map_pages_local(pgd_t *pgdir, unsigned long virt, struct page **pages,
+ int nr_pages)
+{
+ return 0;
+}
+#endif
+
/**
* pcpu_map_pages - map pages into a pcpu_chunk
* @chunk: chunk of interest
@@ -224,6 +280,13 @@ static int pcpu_map_pages(struct pcpu_chunk *chunk,
if (err < 0)
goto err;

+ err = __pcpu_map_pages_local(percpu_pgd[cpu],
+ (unsigned long)chunk->local_base + (page_start << PAGE_SHIFT),
+ &pages[pcpu_page_idx(cpu, page_start)],
+ page_end - page_start);
+ if (err < 0)
+ goto err;
+
for (i = page_start; i < page_end; i++)
pcpu_set_page_chunk(pages[pcpu_page_idx(cpu, i)],
chunk);
@@ -233,6 +296,9 @@ static int pcpu_map_pages(struct pcpu_chunk *chunk,
for_each_possible_cpu(tcpu) {
__pcpu_unmap_pages(pcpu_chunk_addr(chunk, tcpu, page_start),
page_end - page_start);
+ __pcpu_unmap_pages_local(percpu_pgd[cpu],
+ (unsigned long)chunk->local_base + (page_start << PAGE_SHIFT),
+ page_end - page_start);
if (tcpu == cpu)
break;
}
@@ -258,6 +324,11 @@ static void pcpu_post_map_flush(struct pcpu_chunk *chunk,
flush_cache_vmap(
pcpu_chunk_addr(chunk, pcpu_low_unit_cpu, page_start),
pcpu_chunk_addr(chunk, pcpu_high_unit_cpu, page_end));
+
+#ifdef CONFIG_HAVE_LOCAL_PER_CPU_MAP
+ flush_cache_vmap((unsigned long)chunk->local_base + (page_start << PAGE_SHIFT),
+ (unsigned long)chunk->local_base + (page_end << PAGE_SHIFT));
+#endif
}

/**
@@ -349,6 +420,24 @@ static struct pcpu_chunk *pcpu_create_chunk(gfp_t gfp)
chunk->data = vms;
chunk->base_addr = vms[0]->addr - pcpu_group_offsets[0];

+#ifdef CONFIG_HAVE_LOCAL_PER_CPU_MAP
+ unsigned long delta = (unsigned long)chunk->base_addr - (unsigned long)pcpu_base_addr;
+ unsigned long hint = delta + (unsigned long)pcpu_local_base;
+ struct vm_struct *local_vm = pcpu_get_local_vm_area(hint,
+ pcpu_unit_size, pcpu_atom_size);
+ if (!local_vm) {
+ pcpu_free_vm_areas(vms, pcpu_nr_groups);
+ pcpu_free_chunk(chunk);
+ return NULL;
+ }
+
+ chunk->local_base = local_vm->addr;
+ chunk->local_data = (void *)local_vm;
+#else
+ chunk->local_base = 0;
+ chunk->local_data = NULL;
+#endif
+
pcpu_stats_chunk_alloc();
trace_percpu_create_chunk(chunk->base_addr);

@@ -365,6 +454,8 @@ static void pcpu_destroy_chunk(struct pcpu_chunk *chunk)

if (chunk->data)
pcpu_free_vm_areas(chunk->data, pcpu_nr_groups);
+ if (chunk->local_data)
+ free_vm_area((struct vm_struct *)chunk->local_data);
pcpu_free_chunk(chunk);
}

diff --git a/mm/vmalloc.c b/mm/vmalloc.c
index 8ef7d9987e18..f224ffec5696 100644
--- a/mm/vmalloc.c
+++ b/mm/vmalloc.c
@@ -4836,17 +4836,21 @@ pvm_find_va_enclose_addr(unsigned long addr)
* in - the VA we start the search(reverse order);
* out - the VA with the highest aligned end address.
* @align: alignment for required highest address
+ * @pcpu: whether request allocation from local percpu area
*
* Returns: determined end address within vmap_area
*/
static unsigned long
-pvm_determine_end_from_reverse(struct vmap_area **va, unsigned long align)
+pvm_determine_end_from_reverse(struct vmap_area **va, unsigned long align, bool pcpu)
{
unsigned long vmalloc_end;
unsigned long addr;

#ifdef CONFIG_HAVE_LOCAL_PER_CPU_MAP
- vmalloc_end = PERCPU_END & ~(align - 1);
+ if (pcpu)
+ vmalloc_end = LOCAL_PERCPU_END & ~(align - 1);
+ else
+ vmalloc_end = PERCPU_END & ~(align - 1);
#else
vmalloc_end = VMALLOC_END & ~(align - 1);
#endif
@@ -4955,7 +4959,7 @@ struct vm_struct **pcpu_get_vm_areas(const unsigned long *offsets,
end = start + sizes[area];

va = pvm_find_va_enclose_addr(vmalloc_end);
- base = pvm_determine_end_from_reverse(&va, align) - end;
+ base = pvm_determine_end_from_reverse(&va, align, false) - end;

while (true) {
/*
@@ -4976,7 +4980,7 @@ struct vm_struct **pcpu_get_vm_areas(const unsigned long *offsets,
* base downwards and then recheck.
*/
if (base + end > va->va_end) {
- base = pvm_determine_end_from_reverse(&va, align) - end;
+ base = pvm_determine_end_from_reverse(&va, align, false) - end;
term_area = area;
continue;
}
@@ -4986,7 +4990,7 @@ struct vm_struct **pcpu_get_vm_areas(const unsigned long *offsets,
*/
if (base + start < va->va_start) {
va = node_to_va(rb_prev(&va->rb_node));
- base = pvm_determine_end_from_reverse(&va, align) - end;
+ base = pvm_determine_end_from_reverse(&va, align, false) - end;
term_area = area;
continue;
}
@@ -5149,6 +5153,61 @@ void pcpu_free_vm_areas(struct vm_struct **vms, int nr_vms)
free_vm_area(vms[i]);
kfree(vms);
}
+
+#ifdef CONFIG_HAVE_LOCAL_PER_CPU_MAP
+/* Find free vm area starts from hint */
+struct vm_struct *pcpu_get_local_vm_area(unsigned long hint,
+ int unit_size, size_t align)
+{
+ struct vmap_area *tmp_va, *va;
+ struct vm_struct *vm;
+ struct vmap_node *vn;
+ unsigned long end;
+ int ret;
+
+ va = kmem_cache_zalloc(vmap_area_cachep, GFP_KERNEL);
+ vm = kzalloc(sizeof(struct vm_struct), GFP_KERNEL);
+ if (!va || !vm)
+ goto err_free;
+
+ spin_lock(&free_vmap_area_lock);
+
+ tmp_va = pvm_find_va_enclose_addr(hint);
+ if (!tmp_va)
+ return NULL;
+
+ end = pvm_determine_end_from_reverse(&tmp_va, align, true);
+
+ if (hint + unit_size > end)
+ return NULL;
+
+ ret = va_clip(&free_vmap_area_root,
+ &free_vmap_area_list, tmp_va, hint, unit_size);
+ if (ret)
+ return NULL;
+
+ va->va_start = hint;
+ va->va_end = hint + unit_size;
+
+ spin_unlock(&free_vmap_area_lock);
+
+ vn = addr_to_node(va->va_start);
+
+ spin_lock(&vn->busy.lock);
+ insert_vmap_area(va, &vn->busy.root, &vn->busy.head);
+ setup_vmalloc_vm(vm, va, VM_ALLOC,
+ pcpu_get_local_vm_area);
+ spin_unlock(&vn->busy.lock);
+
+ return vm;
+
+err_free:
+ kmem_cache_free(vmap_area_cachep, va);
+ kfree(vm);
+
+ return NULL;
+}
+#endif
#endif /* CONFIG_SMP */

#ifdef CONFIG_PRINTK
--
2.47.0