From: Yang Shi <yang@os.amperecomputing.com>
To: cl@gentwo.org, dennis@kernel.org, tj@kernel.org,
urezki@gmail.com, catalin.marinas@arm.com, will@kernel.org,
ryan.roberts@arm.com, david@kernel.org,
akpm@linux-foundation.org, hca@linux.ibm.com, gor@linux.ibm.com,
agordeev@linux.ibm.com
Cc: yang@os.amperecomputing.com, linux-mm@kvack.org,
linux-arm-kernel@lists.infradead.org,
linux-kernel@vger.kernel.org
Subject: [PATCH 14/16] mm: percpu: allocate and free local percpu vm area
Date: Wed, 15 Jul 2026 11:04:16 -0700 [thread overview]
Message-ID: <20260715180455.515692-15-yang@os.amperecomputing.com> (raw)
In-Reply-To: <20260715180455.515692-1-yang@os.amperecomputing.com>
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 | 94 +++++++++++++++++++++++++++++++++++++++++
mm/vmalloc.c | 92 +++++++++++++++++++++++++++++++++++++---
4 files changed, 185 insertions(+), 5 deletions(-)
diff --git a/include/linux/vmalloc.h b/include/linux/vmalloc.h
index d87dc7f77f4e..daf9965a111d 100644
--- a/include/linux/vmalloc.h
+++ b/include/linux/vmalloc.h
@@ -313,6 +313,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 ec2e55a2f836..4d461305243b 100644
--- a/mm/percpu-internal.h
+++ b/mm/percpu-internal.h
@@ -70,6 +70,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..5decaad7640f 100644
--- a/mm/percpu-vm.c
+++ b/mm/percpu-vm.c
@@ -112,6 +112,10 @@ static int pcpu_alloc_pages(struct pcpu_chunk *chunk,
return -ENOMEM;
}
+#ifdef CONFIG_HAVE_LOCAL_PER_CPU_MAP
+extern pgd_t *percpu_pgd[NR_CPUS];
+#endif
+
/**
* pcpu_pre_unmap_flush - flush cache prior to unmapping
* @chunk: chunk the regions to be flushed belongs to
@@ -130,6 +134,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 +146,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(unsigned int cpu, unsigned long virt,
+ int nr_pages)
+{
+ __vunmap_range_noflush(percpu_pgd[cpu], virt, virt + (nr_pages << PAGE_SHIFT));
+}
+#else
+static void __pcpu_unmap_pages_local(unsigned int cpu, unsigned long virt,
+ int nr_pages)
+{
+ return;
+}
+#endif
+
/**
* pcpu_unmap_pages - unmap pages out of a pcpu_chunk
* @chunk: chunk of interest
@@ -166,6 +189,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(cpu,
+ (unsigned long)chunk->local_base + (page_start << PAGE_SHIFT),
+ page_end - page_start);
}
}
@@ -188,6 +215,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 +230,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(unsigned int cpu, 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(percpu_pgd[cpu], 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(unsigned int cpu, 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 +283,13 @@ static int pcpu_map_pages(struct pcpu_chunk *chunk,
if (err < 0)
goto err;
+ err = __pcpu_map_pages_local(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 +299,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(tcpu,
+ (unsigned long)chunk->local_base + (page_start << PAGE_SHIFT),
+ page_end - page_start);
if (tcpu == cpu)
break;
}
@@ -258,6 +327,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 +423,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 +457,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 9ec401918736..3d94ca4bbd17 100644
--- a/mm/vmalloc.c
+++ b/mm/vmalloc.c
@@ -4923,17 +4923,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
@@ -5042,7 +5046,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) {
/*
@@ -5063,7 +5067,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;
}
@@ -5073,7 +5077,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;
}
@@ -5236,6 +5240,84 @@ 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) {
+ spin_unlock(&free_vmap_area_lock);
+ goto err_free;
+ }
+
+ end = pvm_determine_end_from_reverse(&tmp_va, align, true);
+
+ if (hint + unit_size > end) {
+ spin_unlock(&free_vmap_area_lock);
+ goto err_free;
+ }
+
+ ret = va_clip(&free_vmap_area_root,
+ &free_vmap_area_list, tmp_va, hint, unit_size);
+ if (ret) {
+ spin_unlock(&free_vmap_area_lock);
+ goto err_free;
+ }
+
+ va->va_start = hint;
+ va->va_end = hint + unit_size;
+
+ spin_unlock(&free_vmap_area_lock);
+
+ if (kasan_populate_vmalloc(va->va_start, unit_size, GFP_KERNEL))
+ goto err_free_shadow;
+
+ 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);
+
+ kasan_unpoison_vmap_areas(&vm, 1, KASAN_VMALLOC_PROT_NORMAL);
+
+ return vm;
+
+err_free:
+ kmem_cache_free(vmap_area_cachep, va);
+ kfree(vm);
+
+ return NULL;
+
+err_free_shadow:
+ spin_lock(&free_vmap_area_lock);
+ va = merge_or_add_vmap_area_augment(va, &free_vmap_area_root,
+ &free_vmap_area_list);
+ if (va)
+ kasan_release_vmalloc(va->va_start, va->va_end, va->va_start, va->va_end,
+ KASAN_VMALLOC_PAGE_RANGE | KASAN_VMALLOC_TLB_FLUSH);
+ kfree(vm);
+ spin_unlock(&free_vmap_area_lock);
+ return NULL;
+
+}
+#endif
#endif /* CONFIG_SMP */
#ifdef CONFIG_PRINTK
--
2.47.0
next prev parent reply other threads:[~2026-07-15 18:06 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-07-15 18:04 [RFC v2 PATCH 0/16] Optimize this_cpu_*() ops for non-x86 (ARM64 for this series) Yang Shi
2026-07-15 18:04 ` [PATCH 01/16] drivers: arch_numa: move percpu set up code to arch Yang Shi
2026-07-15 18:04 ` [PATCH 02/16] arm64: kconfig: make percpu related configs not depend on NUMA Yang Shi
2026-07-15 18:04 ` [PATCH 03/16] mm: pgalloc: introduce {pud|pmd}_populate_sync() Yang Shi
2026-07-15 18:04 ` [PATCH 04/16] vmalloc: pass in pgd pointer for vmap{__vunmap}_range_noflush() Yang Shi
2026-07-15 18:04 ` [PATCH 05/16] arm64: mm: enable percpu kernel page table Yang Shi
2026-07-15 18:04 ` [PATCH 06/16] arm64: mm: defined {pud|pmd}_populate_sync() Yang Shi
2026-07-15 18:04 ` [PATCH 07/16] arm64: mm: sync percpu page table for memory hotplug/unplug Yang Shi
2026-07-15 18:04 ` [PATCH 08/16] arm64: kasan: sync up kasan shadow area page table Yang Shi
2026-07-15 18:04 ` [PATCH 09/16] arm64: mm: define percpu virtual space area Yang Shi
2026-07-15 18:04 ` [PATCH 10/16] mm: percpu: prepare to use dedicated percpu area Yang Shi
2026-07-15 18:04 ` [PATCH 11/16] arm64: mm: map local percpu first chunk Yang Shi
2026-07-15 18:04 ` [PATCH 12/16] mm: percpu: set up first chunk and reserve chunk Yang Shi
2026-07-15 18:04 ` [PATCH 13/16] arm64: mm: introduce __per_cpu_local_off Yang Shi
2026-07-15 18:04 ` Yang Shi [this message]
2026-07-15 18:04 ` [PATCH 15/16] arm64: kconfig: select HAVE_LOCAL_PER_CPU_MAP Yang Shi
2026-07-15 18:04 ` [PATCH 16/16] arm64: percpu: use local percpu for this_cpu_*() APIs Yang Shi
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260715180455.515692-15-yang@os.amperecomputing.com \
--to=yang@os.amperecomputing.com \
--cc=agordeev@linux.ibm.com \
--cc=akpm@linux-foundation.org \
--cc=catalin.marinas@arm.com \
--cc=cl@gentwo.org \
--cc=david@kernel.org \
--cc=dennis@kernel.org \
--cc=gor@linux.ibm.com \
--cc=hca@linux.ibm.com \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=ryan.roberts@arm.com \
--cc=tj@kernel.org \
--cc=urezki@gmail.com \
--cc=will@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.